Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying app main menu/home screen when returning to app after having started an external Activity/Intent

I'm starting Android Market via my app to search for similar products using this code:

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://market.android.com/search?q=pub:\"some txt\""));
c.startActivity(intent);

This works fine for showing similar products. However, if I hit the home button while in the market, when I re-open the app it still shows market results. I want to go to the main menu in this case.

Is there a solution?

like image 487
Sukitha Udugamasooriya Avatar asked Mar 16 '10 10:03

Sukitha Udugamasooriya


People also ask

How do I know if an app is running in the background Android?

You can detect currently foreground/background application with ActivityManager. getRunningAppProcesses() which returns a list of RunningAppProcessInfo records. To determine if your application is on the foreground check RunningAppProcessInfo.

Which method is used to launch a new activity or get an existing activity to do something new?

Context.startActivity() The Intent object is passed to this method to launch a new activity or get an existing activity to do something new.

What is app foreground activity?

Foreground services perform operations that are noticeable to the user. Foreground services show a status bar notification, so that users are actively aware that your app is performing a task in the foreground and is consuming system resources.


2 Answers

Sorry, FLAG_ACTIVITY_NO_HISTORY is probably not the correct solution. Note the semantics of it -- the activity just doesn't appear in the history. Thus if the user taps on one of the things in it to go to the next activity, then pressing back, they will not return to the previous one (but the one before). This is rarely what you want.

Worse, if they go to a second activity from the market activity, press home, and return to your app, the second activity will still be there (it is keeping itself in the history).

The correct flag for this situation is FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET.

like image 68
hackbod Avatar answered Oct 20 '22 10:10

hackbod


If you add the FLAG_ACTIVITY_NO_HISTORY flag to the intent, it won't be kept on the history stack. When the user navigates back to your application, the last activity that was visible before you launched the marketplace will be shown.

Intent intent = new Intent(Intent.ACTION_VIEW,
    Uri.parse("http://market.android.com/search?q=pub:\"some txt\"")); 

c.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
c.startActivity(intent); 

Edit: hackbod is correct: FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET is a better fit for what you need.

like image 10
Richard Szalay Avatar answered Oct 20 '22 10:10

Richard Szalay