2016-10-25 4 views
0

最初に、RecyclerViewのレイアウトマネージャを保存して復元する方法について、the accepted answer in this previously posted questionに続きました。アプリケーションがバックグラウンドでプッシュされ再作成されたときに、RecyclerViewがLayoutManager状態を復元しない

IアクティビティB.で私RecyclerViewを有する[注:アクティビティAは、インテントを介してデータを転送し、アクティビティBを起動します。]

ユーザがアクティビティBでviewholderをクリックすると、アクティビティCが起動されます。アクティビティCが閉じて破棄されると、アクティビティBが表示され、RecyclerViewで復元されたすべてのデータが表示されます。

問題: しかし、私のアプリから離れて移動すると(ホームキーを押すと)、バックグラウンドにプッシュされます。私のアプリが復活すると、onCreate()が再び呼び出されます。 LayoutManagerの状態は保存されていますが、ですが、RecyclerViewに表示されているデータはありません。

一部のデバッグ後にレイアウトマネージャの状態が保存されたことがわかります。

Saved Instance State = Bundle[{ActivityB - Linear Layout [email protected]358f8f, android:viewHierarchyState=Bundle[mParcelledData.dataSize=1296]}] 

私がしようとする時間後にこの問題を解決する方法がわからないです:のonCreate()は、背景からアプリを復活さの後に再び呼び出されたときには、このとして表示されます。

マイコード:

public class ActivityB extends AppCompatActivity { 

     private RecyclerView recyclerView; 
     private LinearLayoutManager linearLayoutManager; 
     private Parcelable linearLayoutState; 
     private RecyclerAdapter adapter; 
     private Button more; 
     private String id; 
     private List<VideoItem> list; 
     private List<VideoItem> newList; 
     private final String LINEARLAYOUT_STATE_KEY = "Activity B - Linear Layout State"; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_blayout); 

      // Retrieving data from Activity A 
      Intent intent = getIntent(); 
      Bundle bundle = intent.getBundleExtra("bundle"); 
      someClass.innerClass class = bundle.getParcelable("data"); 
      id = class.getID(); 
      list = class.getList(); 
      newList = new ArrayList<>(); 

      // Instantiating Recycler View 
      recyclerView = (RecyclerView)findViewById(R.id.activityb_recyclerview); 
      recyclerView.setNestedScrollingEnabled(false); 

      linearLayoutManager = new LinearLayoutManager(context); 
      recyclerView.setLayoutManager(linearLayoutManager); 

      adapter = new RecyclerAdapter(); 
      recyclerView.setAdapter(adapter); 

      moreButton = (Button)findViewById(R.id.activityb_more); 
      moreButton.setOnClickListener(new fetchMoreClickListener()); 
     } 

     @Override 
     public void onResume(){ 
       if (linearLayoutState != null) { 
       linearLayoutManager.onRestoreInstanceState(linearLayoutState); 
       } else { 
       // If no layout state is found, I need to populate my 
       // adapter. 
       // Here I am simply cloning my list again, to keep the original 
       // list intact. generateSubset() removes items from the cloned list 
       // to create a smaller subset that is added to the adapter. 
       for (ListItem item : list){ 
         newList.add(new ListItemi(item)); 
       } 
       adapter.addItems(generateSubset(0)); 
       } 
       super.onResume(); 
     } 

     @Override 
     protected void onSaveInstanceState(Bundle outState) { 
       linearLayoutState = linearLayoutManager.onSaveInstanceState(); 
       outState.putParcelable(LINEARLAYOUT_STATE_KEY, linearLayoutState); 
       super.onSaveInstanceState(outState); 
     } 

     @Override 
     protected void onRestoreInstanceState(Bundle savedInstanceState) { 
       super.onRestoreInstanceState(savedInstanceState); 
       linearLayoutState = savedInstanceState.getParcelable(LINEARLAYOUT_STATE_KEY); 
     } 

他のアクティビティのライフサイクル・メソッドがオーバーライドされていません。エラーメッセージは表示されません。私のビューアに保存されたデータは、もはやRecyclerViewに表示されません。

編集1:ここでは

は私RecyclerAdapterコードです:

パブリッククラスRecyclerAdapterは今RecyclerView.Adapter { プライベート静的日を延長します。 プライベートリスト一覧;

public static class ItemHolder extends RecyclerView.ViewHolder 
     implements View.OnClickListener { 

    private Context context; 
    private String key; 
    private Bundle itemBundle; 
    private SimpleDraweeView photo; 
    private TextView title; 

    public ItemHolder(Context context, View view){ 
     super(view); 

     this.context = context; 
     this.key = null; 
     this.itemBundle = null; 
     photo = (SimpleDraweeView)view.findViewById(R.id.itemholder_photo); 
     title = (TextView)view.findViewById(R.id.itemholder_title); 
     view.setOnClickListener(this); 
    } 

    public void bindData(Item item){ 
     if (key == null){ key = item.getID(); } 
     if (itemBundle == null){ 
      itemBundle = new Bundle(); 
      itemBundle.setClassLoader(Item.class.getClassLoader()); 
      itemBundle.putParcelable("data", item); 
     } 

     photo.setImageURI(Uri.parse(item.getPhotoURL())); 
    } 

    @Override 
    public void onClick(View view) { 
     Intent intent = new Intent(context, ActivityB.class); 
     intent.putExtra("bundle", itemBundle); 
     context.startActivity(intent); 
    } 
} 

public RecyclerAdapter(){ 
    this.list = new ArrayList<>(); 
    this.now = new Date(); 
} 

public RecyclerAdapter(ArrayList<VideoItem> list){ 
    this.list = list; 
    this.now = new Date(); 
    this.notifyItemRangeInserted(0, list.size()); 
} 

@Override 
public RecyclerAdapter.ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    Context context = parent.getContext(); 
    View recyclerItem = LayoutInflater.from(context) 
      .inflate(R.layout.itemholder_item, parent, false); 

    return new ItemHolder(context, recyclerItem); 
} 

@Override 
public void onBindViewHolder(RecyclerAdapter.ItemHolder holder, int position) { 
    Item item = list.get(position); 
    holder.bindData(item); 
} 

private static Date getNow(){ return now; } 

private static void updateNow(){ now = new Date(); } 

@Override 
public int getItemCount() { 
    return list.size(); 
} 

public void addItem(Item item){ 
    list.add(item); 
    this.notifyItemInserted(list.size() - 1); 
} 

public void addItems(ArrayList<Item> items){ 
    int oldSize = list.size(); 
    list.addAll(items); 
    this.notifyItemRangeInserted(oldSize, items.size()); 
} 

}

+0

あなたRecyclerAdapterを投稿することができますか? –

+0

基本的に、正しく表示されている場合は、LayoutManagerの状態を復元するだけで、アダプタのデータは復元しません。したがって、アプリケーションを再び開くと、空のデータセットを持つアダプタが作成され、RecyclerViewにバインドされます。何をすべきかは、表示するデータを含むリストを受け入れるアダプタのコンストラクタを作成し、それをインスタンス化するときにリストを渡すことです( 'adapter = new RecyclerAdapter(list);')。 –

+0

@BenjaminScharbau RecyclerAdapterが上記で追加されました。したがって、リストをバンドルするのが最善です。if(savedInstanceState == null)trueの場合はadapter = new RecyclerAdapter()を、falseの場合はadapter = new RecyclerAdapter(savedList)を呼び出しますか? – ByteSized

答えて

0

次のようなものを見て、あなたのonResume()方法を変更してみてください:

@Override 
    public void onResume(){ 
      super.onResume(); 
      if (linearLayoutState != null) { 
      linearLayoutManager.onRestoreInstanceState(linearLayoutState); 
      } 
      // Here comes the code to populate your data. 
      // I'm not sure how you do this, so I just copy/paste your code 
      for (ListItem item : list){ 
        newList.add(new ListItemi(item)); 
      } 
      // Now instatiate and add the adapter to the RecyclerView 
      adapter = new RecyclerAdapter(newList); 
      recyclerView.setAdapter(adapter); 
      } 
    } 
+0

ありがとうございました:)私は、アプリがバックグラウンドから戻るときに、RecyclerViewアダプタにデータを再投入していないことも発見しました。 – ByteSized

関連する問題