0

Activity A has a list of categories (RecyclerView) in a fragment. When clicking on an item, a new Activity B is launched, which also contains a list of items (RecyclerView) in a fragment, via an adapter: `

    myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           Intent dynamicCableView = new Intent(view.getContext(), CableOverview.class);
           dynamicItemView.putExtra("systemModel", systemModelList.get(position));
           view.getContext().startActivity(dynamicItemView);
        }`

Clicking on one of those, Activity C, containing data pertaining to that item, is launched, basically like this:

public void goToAnActivity(View view) {
Intent Intent = new Intent(this, AnActivity.class);
startActivity(Intent);
}

When returning from Activity C via the back button to its parent Activity B, the previous state is not stored, so the list of items is gone whereas in my understanding, it should be retrieved from the stack. Instead onDestroy of B (NOT only of C) is called. I do NOT want to preserve data from Activity C but go back to its parent which should maintain its state. It basically should remember which category was selected to display its subset of items. I am using API 25.

I tried: overriding onSaveInstanceState in the fragment of B or in the activity. Doing away with the fragments and putting the lists in the activity itself. However, savedInstanceState is always null when navigating back.

How do I ensure my list can be recreated?

EDIT: Manifest contains:

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.Launcher">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
  • How do you access data of recyclerview items when you launch Activity B ? – gokhan Apr 04 '18 at 12:45
  • I added the code above. – kaliyanei91 Apr 04 '18 at 12:50
  • Could you add your start methods for activityA to ActivityB and activity B to ActivityC. Which method do you use to add fragment at activity ?( add or replace) – gokhan Apr 04 '18 at 12:57
  • I doesn't work even without fragments. It doesn't work even in trivial examples with a button OnClickListener and then navigating back. The parent activity always gets destroyed and is irretrievable. – kaliyanei91 Apr 04 '18 at 13:07
  • Do you define custom launcher mode in manifest ? – gokhan Apr 04 '18 at 13:15
  • see edit above :) That is the only stuff concerning the launcher, but it's only for the MainActivity, not A,B or C. – kaliyanei91 Apr 04 '18 at 13:22
  • Ok. Lastly could you share your ActivityB' s onCreate() and Fragment's onCreate() methods – gokhan Apr 04 '18 at 13:30

1 Answers1

0

I found the solution when realizing that the problem actually was OnDestroy always getting called when navigating back. Thanks to @gokhan, I knew it was related to the launch mode. According to this answer, Activity B requires this line in the manifest: android:launchMode="singleTop"