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

前端 未结 7 1251
借酒劲吻你
借酒劲吻你 2020-12-09 03:51

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

相关标签:
7条回答
  • 2020-12-09 04:27

    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.

    0 讨论(0)
  • 2020-12-09 04:29

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

    0 讨论(0)
  • 2020-12-09 04:29

    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);
        }
    
    }
    
    0 讨论(0)
  • 2020-12-09 04:33

    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
    
    0 讨论(0)
  • 2020-12-09 04:37

    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.

    0 讨论(0)
  • 2020-12-09 04:37

    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);
    }
    
    0 讨论(0)
提交回复
热议问题