Differentiating between an Activity launch from home screen or from another activity from App

你说的曾经没有我的故事 提交于 2019-12-28 12:28:52

问题


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


回答1:


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.




回答2:


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.




回答3:


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



回答4:


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....




回答5:


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);
    }

}



回答6:


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;
}



回答7:


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);
}


来源:https://stackoverflow.com/questions/5637876/differentiating-between-an-activity-launch-from-home-screen-or-from-another-acti

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!