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

前端 未结 7 1277
借酒劲吻你
借酒劲吻你 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: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 intentCategories = intent.getCategories();
            boolean wasActivityStartedFromLauncherIcon = Intent.ACTION_MAIN.equals(intent.getAction()) && intentCategories != null && intentCategories.contains(Intent.CATEGORY_LAUNCHER);
            boolean showUpButton = !wasActivityStartedFromLauncherIcon;
            actionBar.setDisplayHomeAsUpEnabled(showUpButton);
        }
    
    }
    

提交回复
热议问题