0

I hesitated to ask this question as I don't have code blocks to show. The problem is more conceptual.

I have a baseActivity that handles my ActionBar. The baseActivity is inherited across most Activities so that my ActionBar is accessible from everywhere. The app itself is a shopping app and one of the buttons on the Actionbar is set to "View shopping cart" which is an Activity in a Dialog theme so it looks like a popup over whatever Activity was running when the user pressed the button.

Within the cart, I want to have buttons to give the user quick access to add new items in the catalog. Press a button, start the catalog and let the user shop. That all works fine.

The problem is that if the catalog was the current activity when the dialog was invoked, I'd like to simply dismiss the dialog and go back to the catalog - otherwise, I have one instance of the catalog running and pressing the button causes another instance.

Is it possible to determine what activity was active when a dialog was invoked from the actionbar?

Martin
  • 4,445
  • 4
  • 23
  • 30

2 Answers2

0

You might find intent Flags that can handle the behavior you are looking for. For example, when you build the intent to launch the catalog add: FLAG_ACTIVITY_REORDER_TO_FRONT - this will not create a new activity if it is currently in the task stack and also do the job of bringing an existing activity back to the top where the user left off if the user navigated away from it, but then expected to "return" to it from the cart.

Here's a good link to explain some intent flags: How do you use Intent.FLAG_ACTIVITY_CLEAR_TOP to clear the Activity Stack?

Community
  • 1
  • 1
Jim
  • 9,902
  • 1
  • 23
  • 35
  • Thanks, Jim. That's exactly the needle I was looking for in the haystack of documentation :) – Martin Jan 31 '14 at 17:04
  • the activity I reordered to the front - works great - except it has no history stack. Pressing the back button on that activity takes me back to the launcher screen and leaves the app suspended. Pressing the app icon doesn't launch it fresh, but rather takes me to the activity that would have been in the history had I not reordered. Any quick fix you know of? – Martin Jan 31 '14 at 17:41
  • Are you using other flags? That sounds like "CLEAR_TOP" or another flag is also being used. You might have to try different combinations to get the "expected" result. If not, as you nav through activities you might have to SET_RESULT and track the stack that way, not easy. – Jim Jan 31 '14 at 18:38
0

I presume your base activity inflates a menu xml in the onCreateOptionsMenu method (that's the typical way to put action items on the action bar). If so, you can override that behavior in the one activity which needs to behave differently and simply not show the button:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    menu.removeItem(R.id.action_show_catalog);
    // OR, if you want to be able to show it again in this activity, use
    // MenuItem catalogItem = menu.findItem(R.id.action_show_catalog);
    // item.setVisible(false);
}

It's worth mentioning: The action bar is a window decoration. Each running Activity has its own action bar. That your BaseActivity is doing the work of setting it up does not mean it is the same ActionBar instance across your app.

Karakuri
  • 36,506
  • 12
  • 75
  • 103
  • Yes, it's there in each activity, but that doesn't help determine what activity was on top when the actionbar button was pressed as that code is handled in baseActivity – Martin Jan 31 '14 at 16:31
  • That's why you override `onCreateOptionsMenu ` it in the activity you don't want the dialog to be accessible in. – Karakuri Jan 31 '14 at 18:58