-1

I'm trying to do something similar to this question.

I've got a main app A, and sub-apps B and C. B is a searchable dictionary, and C is a definition, so if you search for many words, you can end up with the stack looking like A > B > C > B > B > C > B > C > C > C... etc

Whenever I go back, I'd like to go back to the original B, so I want the back stack to basically stay at A > B all the time.

I've currently got it set up using an Intent so when the back button is pressed from C, it goes to a new instance of B, but that's obviously not what I want.

Ideas?

Community
  • 1
  • 1
Jdban101
  • 367
  • 1
  • 3
  • 21
  • 1
    Have you looked into the different launchModes outlined [here](http://developer.android.com/guide/topics/manifest/activity-element.html#lmode)? The singleTop, singleTask, or singleInstance modes might provide the behavior you are looking for. – theisenp Jul 22 '11 at 18:14
  • I second that. Look into different flags and see if they can help. Although if you hit back from C, shouldn't it take you to the original b? – Rob Jul 22 '11 at 18:40
  • The flags do almost what I want, except when I try to launch a new B from C, they go back to the original B. I want a new created when launched that way, but the original gone to on a back press. – Jdban101 Jul 22 '11 at 19:10
  • Okay, my example below is REALLY close to this. When I get to get on my desktop later, I'll have a finished example that does that exactly. On B if you load a new B, it'll go back to old B's but on C it'll go to new B. – Rob Jul 23 '11 at 17:55

3 Answers3

2
 @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)  {
        if (splash.sdk < 5 && keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            onBackPressed();
        }

        return super.onKeyDown(keyCode, event);
    }
    //This will make the back button exit the app to the home screen.
    @Override
    public void onBackPressed() {
        moveTaskToBack(true);
        return;
    }
Asish AP
  • 4,421
  • 2
  • 28
  • 50
  • 1
    I think the OP wants to prevent multiple instances of the same activity from being on the Back Stack at the same time, not exit to the Home Screen every time back is pressed. – theisenp Jul 22 '11 at 18:19
  • splash.sdk, eh? Nice to see my example re-used! :D You need to get the SDK version for this, which in that example was easiest to put on the splash screen. `int sdk = new Integer(Build.VERSION.SDK).intValue();` and then you'd just do `sdk < 5 && keycode...` instead. – Rob Jul 22 '11 at 18:47
2

Shouldn't hitting BACK from C take you to the original B? So the stack should look like:

A>
  B>
    C<
  B>
    C>
      C<
    C<
  B<
A

with > going to the next activity, and < being the back button?

You could override onBackPressed for all the activities, so for C it loads a new B, B it loads a new A, and A you would do moveTaskToBack(true);, but that's less of a solution and more of a hack to make it work.

Try to use onResume so that B comes up like it would on default. Set any pertinent variables to what they are in a new activity. If you only want this when you're coming from C, have a class boolean that is set to true when C is loaded, and check it in onResume or onRestart. If it's true, set everything to default/blank, and set the boolean to false. If not, load it how it was (this is if they hit home and come back to the app, basically). I'm not at my work desk, but I think you'll want:

@Override
    public void onResume() {
        super.onResume();
        if(fromC == true){
        //Set all variables to load default dictionary
        fromC = false;
        }
    }
}

This should make the original B act like a new B.

Or instead, you could do it the easy way, and when you call the intent to load a new B like this:

startActivity(getIntent());
finish();

That'll make there only be one B. Making it load as a blank when you go back is a little bit different and requires class variables, or some other tricky trick that I am thinking of. This is something like what you'll want to do: .zip of small sample

If you can get your code reworked to something like that almost (where you can use the appropriate variables to set up things to work right if that makes any sense).

Rob
  • 2,779
  • 5
  • 23
  • 34
  • I just tried: A> B> C< B> C> C< as you said, and it ended up doing as you predicted. The issue comes from when I go to a new instance of B, so: A> B> C> B> C< In this case, I want the last C to go back to the original B, otherwise we can end up with a gigantic back chain. edit: (trying to format this right, having issues) – Jdban101 Jul 22 '11 at 19:04
  • I get what you mean, but why do you want a new instance of B at all? – Rob Jul 22 '11 at 19:10
  • B is basically the main dictionary page, you have all the words on it able to be scrolled through at the start. If you do a search, it then opens B again and shows you all the words that match that search (eg you search for 'a' and all the a's pop up and you can scroll through it). I figure going back to the full scrollable page of all words is best as opposed to exiting and having to reopen if you want that page again. – Jdban101 Jul 22 '11 at 19:18
  • So you can have A> B> B> C< and now you want original B? I'd go with the override on onBackPressed() on all of them for now, and I think the best solution is to figure how to get `onResume()` to load a blank one. Actually, just figure out how to get onResume() to load the 'default' one, it shouldn't be hard. – Rob Jul 22 '11 at 19:22
  • I know exactly what I got wrong here. When you want to search 'A's it opens a new dictionary activity. Instead, have it reload the current one, but make it load for 'A's. I use a lot of class variables in my app, so it made it easy for me to do this in my app. Editing my answer. – Rob Jul 22 '11 at 21:56
  • Edited in how to stop so many B activities from clogging up the stack, as well a small example app that I think should help you. If you can share your code, I could take a look at it. – Rob Jul 22 '11 at 23:00
0

You can set an Intent flag to do this. Say you are in Activity C and you want to call Activity B: you add a flag to the Intent called FLAG_ACTIVITY_CLEAR_TOP. So:

class C extends Activity {

    //...

    private void gotob() {
        Intent go = new Intent(this, B.class);
        go.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(go);
    }

    //...

}

What this does is either to go to the top instance of B in the back stack (and clear everything else off the top), or create a new instance if none exists. I haven't worked out if there is a way to do this with launch flags though—there are some similar modes but I don't think any are identical.

Andrew Wyld
  • 7,133
  • 7
  • 54
  • 96