0

This is how I navigate to each fragment.

Fragment f = new ChartFragment();
f.setArguments(fragbundle);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.containerView, f);                               
ft.addToBackStack(null);
ft.commit();

Now my problem is that when I unlock my screen, it shows the first fragment. How can I start from the fragment which I was last in.

Aparajita Sinha
  • 514
  • 1
  • 10
  • 21

2 Answers2

0

Save and retrive which fragment you're on by using this following code: After you've retrived the fragment id or anything you use to identify a particular fragment, navigate to the fragment.

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putInt("fragmentId", fragId); //change the frag id, according to the fragment you're navigating, for example: you're going to fragment1, then set the fragId to 1, and then if you're going to fragment2, set it to 2. And later retrive the id and check it, if it has value 2, navigate to fragment2
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    int fragmentId = savedInstanceState.getInt("fragmentId");
}
Karun Shrestha
  • 731
  • 7
  • 16
0

You can check my answer here for keep the state of fragments. You can hide/show fragment. No need to replace like your codes above. And if you want to open exaclty the fragment when you quite your app, you can use SharedPreference to store the id/tag of current fragment.

How to save fragment state in android?

Community
  • 1
  • 1
RoShan Shan
  • 2,924
  • 1
  • 18
  • 38