区分主屏幕上的活动启动或来自App的其他活动

时间:2011-04-12 15:38:12

标签: android android-activity stack back launcher

我需要知道一种通用的方法来区分来自启动器的活动调用和来自我的应用内部的其他活动的调用,或活动堆栈上的BACK

任何?这对我来说已经有一段时间了,我需要把它休息一下......

提前致谢 JQCorreia

7 个答案:

答案 0 :(得分:27)

在活动的onCreate中,请致电getIntent()。如果从启动器(主屏幕)启动活动,getAction()的值将为android.intent.action.MAINgetCategories()将返回包含android.intent.category.LAUNCHER类别的集合。 如果活动是从其他地方开始的,则这些值可能是null

答案 1 :(得分:3)

除@ advantej的答案外,您可以将每个开始调用延伸到该活动,为起始意图添加额外内容(例如intent.putExtra("caller", this.getClass().getSimpleName());

在活动的onCreate方法中,您可以查看@advantej建议的内容。

如果发起人不是主屏幕图标,那么你可以进一步检查intent.hasExtra("caller")是否返回true,如果是,那么它是什么。

答案 2 :(得分:1)

你可以从意图标志中找到它。

第1步:

if flag  =  Intent.FLAG_ACTIVITY_NEW_TASK 
  launch from other app or activities
else 
  launch from home page

第2步:

{{1}}

答案 3 :(得分:0)

2个案例中的onRestart();当活动来自背景时,2.当用户通过后退按钮到达活动时,然后采样解决方案: 使用onBackPressed()函数设置一个标志..所以你知道onRestart调用后按钮按下... 在onRestart()中检查标志并重置它....

答案 4 :(得分:0)

基于advantej's answer,这是一个完整示例,如果活动是从启动器图标启动的,也会隐藏UP按钮:

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

}

答案 5 :(得分:0)

这是方便的方法,所以你不需要自己编写:

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

答案 6 :(得分:0)

我能想到的最简单的方法是在从您自己的活动中启动活动时传递一个标志。您还应该检查活动是已创建还是已恢复,可以通过在onCreate方法中设置一个布尔值,然后在onResume上对其进行检查来完成。

以下是您可以使用的代码(未经测试):

您要检查的活动(例如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;

    ....

}

从其他活动中调用MainActivity时,请使用以下代码:

private void call_main () {
    Intent i = new Intent(getApplicationContext(), MainActivity.class);
    i.putExtra("call_from_own_activity", true);
    startActivity(i);
}
相关问题