2

I have two fragments in main Activity (1st fragment, 2nd fragment). I replace the 1st fragment with the container view in the onCreate method of main activity.

In the 2nd fragment there is a ListView and when clicking an item on it, a new Activity starts as a detail.

But when I come back to the main Activity, the 1st fragment shows because of the It is in onCreate event.

I tried to use backstack as if backstack had my last fragment then show it. But in the onCreate method the back stack was always null.

Here is my onCreate and replaceFragments method codes:

@Override
protected void onCreate(Bundle savedInstanceState) {

    Log.d("activity", "on create called");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    android.support.v4.app.FragmentManager fm = getSupportFragmentManager();

    int backStackCount = fm.getBackStackEntryCount();
    if (backStackCount > 0){
        for (int entry = 0; entry < fm.getBackStackEntryCount(); entry++) {
            Log.i("back stack", "Found fragment: " + fm.getBackStackEntryAt(entry).getName());
        }
    } else {
        Log.i("back stack", "backstack count is null");
        MainFragment mainFragment = new MainFragment();
        replaceFragments(mainFragment);
    }

    mActionBarMainBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           // setButtonsState(mActionMainBtnLineView);

            if(mMainFragment == null){
                mMainFragment = new MainFragment();
            }

            replaceFragments(mMainFragment);
        }
    });

    mActionBarTransBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           // setButtonsState(mActionTransactionsBtnLineView);

            if(mTransactionsFragment == null){
                mTransactionsFragment = new TransactionsFragment();
            }

            replaceFragments(mTransactionsFragment);
        }
    });
}

public void replaceFragments(Fragment fragment){
    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
    String fragmentName = fragment.getClass().getName();

    fragmentManager.beginTransaction()
            .replace(R.id.container, fragment, fragmentName)
            .addToBackStack(fragmentName)

    .commit();

    fragmentManager.executePendingTransactions();

    if (fragmentName.equals(MainFragment.class.getName())) {
        setButtonsState(mActionMainBtnLineView);
    } else {
        setButtonsState(mActionTransactionsBtnLineView);
    }
}

How to get back stack values in onCreate method when come from an another activity?

My screens Screens

Jonathan Eustace
  • 2,469
  • 12
  • 31
  • 54
Mehmet
  • 1,467
  • 1
  • 14
  • 19

2 Answers2

1

You don't need to do that much things in order to keep your Fragment Stack when you restart your activity. Your Fragment's Stack will be stored in the in the First Activity's SavedInstanceState Bundle. What you need to do it is, When user taps on Back Button, you need to check if the savedInstanceState is Null or not in onCreate of the Activity. If it's Null then your Activity is being created for the first time, if it's not then don't do anything. Check the code below to see how you should implement it.

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

    android.support.v4.app.FragmentManager fm = getSupportFragmentManager();

    if (savedInstanceState == null) {
         MainFragment mainFragment = new MainFragment();
         replaceFragments(mainFragment);
    }
}
osayilgan
  • 5,873
  • 7
  • 47
  • 68
  • Hi osayilgan, thank you for the answer. I implemented your code but savedInstanceState is always null when I come back to the activity. – Mehmet May 21 '15 at 16:45
  • Do you finish your activity when you start the second one ? – osayilgan May 21 '15 at 16:50
  • I just realized somthing. When I come back from the SettingsActivity onCreate method is not called. Just onResume called and MainActivty goes on from its last state. – Mehmet May 21 '15 at 17:34
  • Alright then. So there a couple of different scenarios and ways that you can handle it. The reason that you activity's onCreate is not called can be found [here](http://developer.android.com/training/basics/activity-lifecycle/recreating.html). – osayilgan May 21 '15 at 18:39
  • So when you want to make sure that your applications' behaviour won't change after what ever the reason is, then you need to handle onResume, onSaveInstanceState and onRetainInstanceState as well. – osayilgan May 21 '15 at 18:41
  • So, save your Activity' state in onSaveInstanceState method (when you start another activity, this will be called to save your data), check if your onCreate is Called, to retain the data from Bundle (Attach the first fragment if the Bundle is null), and handle that data in onResume of your activity. – osayilgan May 21 '15 at 18:44
  • I used the onSaveInstanceState and it is called when swtiching the DetailActivity, but when I come back Bundle is still null in the onCreate method and also onRestoreInstanceState is not called. I going to be crazy :) – Mehmet May 21 '15 at 19:25
  • I found another isue. The problem occurs when I press the back button in the action bar. But when I press the device back button last state showns in the MainActivity :) What should I do? – Mehmet May 21 '15 at 19:40
  • Okay fount it :) In the parent activity I needed to add android:launchMode="singleTop" now problem is solved. Thank you very much @osayilgan – Mehmet May 21 '15 at 19:46
1

I found the solution for this isue.

When press the back button in the action bar, the returning activity's onCreate method called and bundle is null to handle the fragments.

To prevent this I added android:launchMode="singleTop" to my parent activty in manifest file.

Example manifest usage

<activity
        android:name=".MainActivity"
        android:launchMode="singleTop" //Here is what I did.
        android:label="@string/app_name"
        android:theme="@style/MyWalletTheme"
        android:uiOptions="splitActionBarWhenNarrow" >
        <meta-data
            android:name="android.support.UI_OPTIONS"
            android:value="splitActionBarWhenNarrow" />

        <intent-filter>
            <action android:name="android.intent.action.MAINACTIVITY" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
Mehmet
  • 1,467
  • 1
  • 14
  • 19