4

I have a problem with the usage of the Up button in my application. There are many posts that I already read dealing with similar problems, but none of them addresses the same issue that I want to solve.

I have two activities - A and B. A displays a list of tasks for a chosen category from an SQLite db. Each task belongs to a category, therefore, A has a private variable topicId passed from an Intent (it acts as a filter). When I click on a task, it opens activity B which allows you to edit it. B has an Up button which leads you back to A (the list with the tasks).

Since A gets the topicId from an intent, When I click the Up button in B, there is no intent in A and the topicId is -1 by default. I thought it's easy to preserve the id - just save it like this:

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("topicId", this.TOPIC_ID);
}

In the onCreate() method I used:

if(savedInstanceState != null) {
    this.TOPIC_ID = savedInstanceState.getInt("topicId");
}

However, the savedInstanceState that gets passed to onCreate() is always null. I put Log in A's lifecycle methods to see what happens.

  1. I'm in A - onCreate() runs and the bundle is null
  2. I click on a task in the list (a Fragment, that's loaded in a ViewPager)
  3. onSaveInstanceState() runs and saves my Id
  4. Now I'm in B and I click the Up button (not the back key on the phone)
  5. It runs A's onDestroy(), then A's onCreate() and the bundle is null!

Can someone tell me why the Bundle in onCreate() is null in this case? I've set B's parent activity to be A in the manifest.xml. I also read a solution which supposes you to add android:launchMode="singleTop" in the manifest for A.

This is, however, not what I want - if it is set to singleTop, the parent activity does NOT get destroyed when you click the Up button. I want to have it destroyed and recreated, because in B I make changes to a task which I want to see immediately reloaded from the DB in A when I click Up.

I'd appreciate if someone tells me why Android passes a null bundle to onCreate(). I already tried adding this code to B and it didn't work:

Intent intent = NavUtils.getParentActivityIntent(this);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
NavUtils.navigateUpFromSameTask(this);

I think this post is about the same issue , but it hasn't been answered for 4 years...

I will post code if you need it. I don't want to be too overwhelming in the beginning.



Edit

After some more search I found this solution: link

It's said:

When you press back, the Activity is destroyed and the data it had is lost. After this you will always get null in the bundle as a new, fresh instance is being created when you reopen the app.

The Bundle savedInstanceState is used when the activity is being recreated due to some changes (like rotation), or because it was paused in the background for a long time.

If you want to persist some data, consider SharedPreferences for small stuff, or maybe a database (SQLite, Realm) or files for large stuff.

As far as I understand, the Bundle is used by Android only in some specific cases, like when you turn off/on the screen, click the Home button or change the screen orientation. In cases like mine, you have to rely on some other features like the SharedPreferences class. I welcome people with some more advanced knowledge than me to shed some more light on the matter.

castafiore
  • 81
  • 8
  • How did you started activity B? SaveInstanceState is something Android do when it needs memory and kills activities to free up memory/ – Uriel Frankel May 03 '18 at 08:51
  • In activity A I have a Fragment with a RecyclerView and inside it multiple CardViews for the tasks. I have attached an onClickListener on the tasks and when one is clicked, the following code handles it: `@Override` `public void onItemClick(int id) {` `Intent intent = new Intent(getActivity(), CreateTaskActivity.class);` `intent.putExtra(CreateTaskActivity.EXTRA_TASK_ID, id);` `startActivity(intent);` `}` – castafiore May 03 '18 at 10:59
  • what is the android:launchMode of activity b? – Uriel Frankel May 03 '18 at 15:05
  • It is not specified, so it must be the default value `standard`. – castafiore May 04 '18 at 06:24
  • the back button destroy an activity, the best method is to use SharedPreferences for this. – maxwellnewage Jun 08 '19 at 18:23

0 Answers0