1

I have a problem with back navigation since it does not restore the last fragment. All activities are AppCompatActivity using navigation_drawer(ActivityA) and scroll_activity(ActivityB) template of Android Studio

  • ActivityA shows fragment_1 (OK)
  • Replace fragment_1 with fragment_2 (OK)
  • Start ActivityB by clicking a button in fragment_2 (OK)
    • If I click system' back button, I can see ActivityB (GOOD)
    • If I click the back navigation of ActivityB, then it display fragment_1 rather than fragment_2(NOT GOOD)

What I have tried are:

  1. Create a static variable to control which fragment is shown, and try to render again at onResume() but it doesnot work.
  2. Followed this link but not work also.

Any help? Thanks

T D Nguyen
  • 7,054
  • 4
  • 51
  • 71
  • Have you tried `setRetainInstance(true)` in fragment_2? – Mustansar Saeed Jan 21 '16 at 11:07
  • Where to use this? I place that in `onCreateView` and it has no effect. – T D Nguyen Jan 21 '16 at 11:12
  • If you customize the back navigation while using fragment use this line in OnBackpressed in Activity .Fragment currentfragment=getSupportFragmentManager().findFragmentById(R.id.view);which gives current visible fragment instance... if(currentfragment instanceof yourFragment){ //your code } – Vishwa Jan 21 '16 at 11:20
  • @NguyenDoanTung: in `onCreateView` of `fragment_2` add `setRetainInstance(true)` – Mustansar Saeed Jan 21 '16 at 11:34

2 Answers2

1

Hi Nguyen i created the same scene but getting fragment_2 on top after back from activityB

This my code `public class FirstActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_first);
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment1()).commit();
    }
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment1 extends Fragment {

    public PlaceholderFragment1() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_first, container, false);
        Button button = (Button) rootView.findViewById(R.id.btn);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                getActivity().getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment2()).commit();
            }
        });
        return rootView;
    }
}

public static class PlaceholderFragment2 extends Fragment {

    public PlaceholderFragment2() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_second, container, false);
        Button button = (Button) rootView.findViewById(R.id.btn);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), Secondctivity.class);
                startActivity(intent);
            }
        });
        return rootView;
    }
}

} `

This my Manifest File

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".FirstActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".Secondctivity"
        android:label="@string/title_activity_secondctivity" >
    </activity>
</application>
Manish
  • 1,071
  • 2
  • 10
  • 27
  • I may try this, but `ActionBarActivity` is deprecated. Currently, I use `AppCompatActivity` and Android Studio provided templates. – T D Nguyen Jan 21 '16 at 11:24
1

I forget one simple solution is Activity Launch Mode. I modified activity in the Manifest file like this and everything works normally.

 <activity
            android:name=".ActivityA"
            android:launchMode="singleTask"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">

The problem is the back navigation button tried to create the ActivityA again (maybe it is because of scroll activity template behaviour).

T D Nguyen
  • 7,054
  • 4
  • 51
  • 71