2

I'm new to Android development, and what I playing around with is a sports type app with a app flow like: League -> Team -> Player -> Player stats (this is in a ViewPager). All using Fragments.

I have the flow working in this direction, but I'm trying to use home button to navigate back up the stack (back button works too). My problem is that going forward through the flow I pass the ID of the League, then Team, then Player, etc... But when the home button is pressed, this data is no longer available.

I've tried setting retainInstance to true, but that doesn't do it. Not sure why, but the ID fields are all null in onCreate whenever I press back or the home button.

I've also tried overriding onSaveInstance and onActivityCreated, and putting the ID's for each entity in the bundle there, but even though I save the ID in the bundle in onSaveInstance, the bundle is null in onActivityCreated.

How can I keep the ID's of each entity around for when the user hits the back or home buttons?

Thanks for any help.

Townsfolk
  • 1,282
  • 1
  • 9
  • 21
  • So back should really be switching the focused fragment of the ViewPager? Make sure that is what's really happening and not a new isntance of the activity being created. You don't show how you're doing the up navigation, nor how you're passing the League, Team, etc. ids. – frozenkoi Nov 02 '13 at 01:35
  • 3
    The `NavUtils` are generally used for inter-activity navigation patterns - from the sounds of it, that's not what you're doing here. Regarding your actual question: `navigateUpFromSameTask()` set the `FLAG_ACTIVITY_CLEAR_TOP` flag. If your activity isn't flagged as `singleTop` or `singleTask`, then that means it will simply get destroyed and recreated freshly, flushing out any state you had saved. I think you should elaborate a bit more on what you expect to happen when navigating 'up' (presumably that's what you refer to with 'home') - some code and/or diagram could help. – MH. Nov 02 '13 at 02:56
  • @frozenkoi Sorry, no. Just the last view is the ViewPager. – Townsfolk Nov 02 '13 at 03:50
  • @MH. I'm working on getting an example up on GitHub, but since I'm new to Android it's taking a while to pull out the important bits. Each of the views mentioned are actually separate Activities, each with a single Fragment for the UI itself. I will get that example up asap. – Townsfolk Nov 02 '13 at 03:51
  • Ok. I've uploaded an example project to [github](https://github.com/townsfolk/testroids). Sorry, it's probably more complex than you were hoping, but I couldn't figure out how to cut it down more without faking out the ID's in a way to skew the results. If you checkout the project, and run it. If you press "Baseball", then "Team One" and press the "home" button, you'll notice the title is blank when it should say "Baseball". For some reason the "back" button does work. ?? – Townsfolk Nov 02 '13 at 06:30
  • Are you seeing the same behavior when using the back button? What happens if you press back after pressing home? (if at the team activity you press home, then back, do you go to league then again to team?). If so, it seems that you're creating a new activity when going up. Make sure that when you go back, you're actually going back and not opening a new activity of the parent type without the id in the arguments. Alternatively, why not add the required id to the parent activity that is being navigated to? – frozenkoi Nov 02 '13 at 10:18
  • See this question http://stackoverflow.com/questions/2442713/view-the-tasks-activity-stack for some ways to visualize the activity stack to see if what you're thinking is happening is what is actually happening. – frozenkoi Nov 02 '13 at 10:23
  • @frozenkoi I'm not sure what you mean by adding the id to the parent activity? it's the ID that's supposed to already be in the parent activity that is null. I will take a look at the link you gave me and see if I can figure out what's going on. For the other question, the back button works like I would expect. If I get to the TeamActivity and hit "home", then "back", I get to the main activity the "LeaguesActivity". – Townsfolk Nov 03 '13 at 03:47
  • @frozenkoi I ran adb, I see what looks a new LeagueActivity (The one that shows the Teams). Code paste didn't work, here's the pastebin: [http://pastebin.com/YeEsZr3y](http://pastebin.com/YeEsZr3y) – Townsfolk Nov 03 '13 at 04:14
  • @frozenkoi It looks like you're right and the home button is creating a new instance of the Activity. How do I get around that - I was trying avoid using something like shared-preferences, but maybe that's the best way to go? – Townsfolk Nov 03 '13 at 04:28
  • 1
    Hope these two questions might point you in the right direction: http://stackoverflow.com/questions/12276027/how-can-i-return-to-a-parent-activity-correctly http://stackoverflow.com/questions/13293772/how-to-navigate-up-to-the-same-parent-state Launch modes for activities in the manifest: http://developer.android.com/guide/topics/manifest/activity-element.html#lmode might help you, in particular Intent.FLAG_ACTIVITY_CLEAR_TASK. You might have to use the `getParentActivityIntent()` function and then modify the intent. – frozenkoi Nov 03 '13 at 06:33
  • 1
    And also this one, which seems to look for almost the same as you: http://stackoverflow.com/questions/15559838/actionbar-up-navigation-recreates-parent-activity-instead-of-onresume?rq=1 – frozenkoi Nov 03 '13 at 06:38
  • Turns out @MH. was correct. I needed to add the singleTop launch mode. Thank you frozenkoi as for pointing me to the documentation. – Townsfolk Nov 05 '13 at 01:03

2 Answers2

2

I had a similar problem when I add the Settings Activity. I had to modify the onMenuItemSelected created automatically.

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
    int id = item.getItemId();
    if (id == android.R.id.home) {
        if (!super.onMenuItemSelected(featureId, item)) {
            // I removed this line
            //NavUtils.navigateUpFromSameTask(this);
            // and add this 3 lines
            Intent intent = NavUtils.getParentActivityIntent(this);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            NavUtils.navigateUpTo(this, intent);
        }
        return true;
    }
    return super.onMenuItemSelected(featureId, item);
}
0

navigateUpFromSameTask() not only set the FLAG_ACTIVITY_CLEAR_TOP flag. It also clears the savedInstanceState. I solved this issue by storing the values that goes normaly in saveInstanceState in SharedPreferences. Storing them in onPause() and restoring in onResume().

PageMaker
  • 395
  • 1
  • 3
  • 10