2

I'm trying to implement a good Up navigation on my Android app.

For instance I want to go from DetailsActivity to MasterActivity. To do so I use the following code:

final Intent upIntent = new Intent(this, MasterActivity.class);
upIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
supportNavigateUpTo(upIntent);

It works great if MasterActivity is already open in the background: it closes the current activity (and all the activities in the stack over MasterActivity) and fires the method onNewIntent on MasterActivity.

But, if MasterActivity is not in the stack, the method simply closes DetailsActivity and nothing else happens.

What I would like to achieve is that, if MasterActivity is not in the stack, the current aActivity gets closed and a new instance of MasterActivity gets created.

I already tried, with no success, the following flags combination:

Intent.FLAG_ACTIVITY_SINGLE_TOP
Intent.FLAG_ACTIVITY_CLEAR_TOP
Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP
Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK
Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK

Have you any idea on how to solve my issue?

Thanks a lot for your time

EDIT

The parent Activity need to be specified at runtime. In fact, the parent Activity can be ActivityA,ActivityB or ActivityC.

Let me better explain the problem. My app manage TV Shows. I have a MainActivty, a TVShowActivity, a SeasonActivity and a EpisodeActivity.

In the navigation drawer from every Activity I want to be able to jump to the parent Activity:

-On EpisodeActivity I want to go to SeasonActivity, TVShowActivity or MainActivity;

-On SeasonActivity I want to go TVShowActivity or MainActivity;

-On TVShowActivity I want to go to MainActivity.

My app currently works if my Activity stack is something like: MainActivty => TVShowActivity => SeasonActivity => EpisodeActivity.

The problem is that sometimes the user can reach EpisodeActivity directly from MainActivity, and I want to be able to use the same navigation pattern.

Spotlight
  • 1,279
  • 1
  • 13
  • 28
  • http://stackoverflow.com/a/12276100/3322203 I think you need a look at that solution. perfect example. – Danial Hussain Nov 17 '14 at 12:41
  • Please see my edits: I need to specify the parent Activity at runtime – Spotlight Nov 17 '14 at 12:49
  • Use Fragments, so you can stick to single Activity – Marcin Orlowski Nov 17 '14 at 12:49
  • you should use `supportShouldUpRecreateTask` as the doc says https://developer.android.com/reference/android/support/v7/app/ActionBarActivity.html#supportNavigateUpTo(android.content.Intent) – MohK Nov 17 '14 at 12:52
  • I cannot completely change my app just to implement that. Beside that, my app is really complex and such a modification would require an incredible amount of time – Spotlight Nov 17 '14 at 12:52
  • @Mohammad supportShouldUpRecreateTask returns false no matter what in my tests – Spotlight Nov 17 '14 at 12:56

1 Answers1

-1

On each activity you can keep for example two buttons providing an onclick event in the properties of their xml and provide intent code in java class:

` public void youronclickname(View v) { Intent myintent = new Intent(your sourceclass, to destination class); startActivity(myintent); } '

similarly try this with your next class

abhiT
  • 1
  • 2
  • 1
    Please read the question! My question asks how to correctly create the Intent, not how to handle the click – Spotlight Nov 17 '14 at 13:15