19

I need to know a generic way to distinguish between a call of activity from launcher and a call from another activity from inside my app, or a BACK on the activity stack

Anyone? this is bugging me for quite a while now and i need to put it to rest...

Thanks in advance JQCorreia

JQCorreia
  • 727
  • 6
  • 15
  • I am not sure, but have you tried to look at `getIntent()` and see if you see some difference?! I guess you should see some differences in the action... – WarrenFaith Apr 12 '11 at 15:40

7 Answers7

33

In the onCreate of your Activity, call getIntent(). If the Activity is started from the launcher (home screen) the values for getAction() will be android.intent.action.MAIN and the getCategories() will return a set which will contain the android.intent.category.LAUNCHER category. If the activity is started from elsewhere these values may be null.

blizzard
  • 5,275
  • 2
  • 34
  • 48
advantej
  • 20,155
  • 4
  • 34
  • 39
  • You're an angel man! Ohh sweet bliss! This is bugging for a shameful(my shame at least) lot of time. – JQCorreia Apr 12 '11 at 16:03
  • Nope not working, still getting android.intent.category.LAUNCHER category when using up navigater. – Warpzit Jun 05 '14 at 09:33
  • I believe this method doesn't work for back/up/finish() cases as the previous Activity is launched with the previous intent that it was started with. This may only apply if the background Activity was destroyed and needs to be recreated. – TheIT Feb 25 '15 at 22:49
  • 4
    Rather than using the String value of `"android.intent.action.MAIN"` you can use the constant `Intent.ACTION_MAIN`. – ban-geoengineering Jun 16 '16 at 09:08
  • Same goes for the `"android.intent.category.LAUNCHER"` String. See my answer for full code example, below: http://stackoverflow.com/a/37855016/1617737 . – ban-geoengineering Jun 16 '16 at 09:24
  • Calling getIntent() on a launcher Activity causes my app to crash. Even if I put it in a try block. –  Dec 21 '19 at 09:00
3

In addition to @advantej's answer, you can extend each start-call to that activity adding an extra to the starting intent (e.g. intent.putExtra("caller", this.getClass().getSimpleName());

In the activity's onCreate method you can check then what @advantej suggests.

If the initiator is not the home-screen icon, than you can check further if the intent.hasExtra("caller") returns true, and if so, what is it.

rekaszeru
  • 19,130
  • 7
  • 59
  • 73
1

You can find it out from intent flag.

step 1:

  Intent intent = getIntent();
  int flag = intent.getFlag();

step 2:

if flag  =  Intent.FLAG_ACTIVITY_NEW_TASK 
  launch from other app or activities
else 
  launch from home page
William
  • 141
  • 5
0

in 2 cases the onRestart(); called, 1.when activity come from background, 2.when the user reach the activity by back button then sample solution: use onBackPressed() function to set a flag.. so u know that onRestart called becouse of back button press... in onRestart () check the flag and reset it and....

Esmaeel Ibraheem
  • 316
  • 2
  • 14
0

Based on advantej's answer, here is a full example that also hides the UP button if the activity was launched from a launcher icon:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sell);

    /**
     * If this activity was started from launcher icon, then don't show the Up button in the action bar.
     */
    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        Intent intent = getIntent();
        Set<String> intentCategories = intent.getCategories();
        boolean wasActivityStartedFromLauncherIcon = Intent.ACTION_MAIN.equals(intent.getAction()) && intentCategories != null && intentCategories.contains(Intent.CATEGORY_LAUNCHER);
        boolean showUpButton = !wasActivityStartedFromLauncherIcon;
        actionBar.setDisplayHomeAsUpEnabled(showUpButton);
    }

}
Community
  • 1
  • 1
ban-geoengineering
  • 18,324
  • 27
  • 171
  • 253
  • 1
    If you want to distinguish between a true start from the launcher and a screen-rotation, then you also need to check whether the given bundle is NULL. – dazed Jul 12 '18 at 19:38
0

Here's convenience method so you don't need to write it yourself:

protected boolean isStartedByLauncher() {
    if (getIntent() == null) {
        return false;
    }
    boolean isActionMain = Intent.ACTION_MAIN.equals(getIntent().getAction());
    Set<String> categories = getIntent().getCategories();
    boolean isCategoryLauncher = categories != null && categories.contains(Intent.CATEGORY_LAUNCHER);
    return isActionMain && isCategoryLauncher;
}
Dmide
  • 6,422
  • 3
  • 24
  • 31
0

The simplest approach that I can think of would be to pass a flag while launching the activity from your own activities. You should also check if the activity was created or resumed, this can be done by setting a boolean in the onCreate method, and then checking it onResume.

Below is the code you can use (not tested):

Activity in which you want to check (say MainActivity.class):

Boolean onCreateCalled = false;
Boolean calledFromAppActivities = false;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    onCreateCalled = true;

    Bundle mainData = getIntent().getExtras();

    if (mainData != null) {
        if (getIntent().hasExtra("call_from_own_activity")) {
            calledFromAppActivities = true;
        }
    }

    .....

}

@Override
protected void onResume() {

    super.onResume();

    if (onCreateCalled && !calledFromAppActivities) {
        // The app was not called from any of our activities.
        // The activity was not resumed but was created.

        // Do Stuff
    }

    // To stop it from running again when activity is resumed.
    onCreateCalled = false;

    ....

}

When calling MainActivity from other activities, use the code below:

private void call_main () {
    Intent i = new Intent(getApplicationContext(), MainActivity.class);
    i.putExtra("call_from_own_activity", true);
    startActivity(i);
}
Hemant Aggarwal
  • 849
  • 1
  • 7
  • 13