区分从主屏幕启动的 Activity 或从应用程序启动的另一个 Activity

发布于 2024-10-31 23:58:40 字数 121 浏览 1 评论 0原文

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

有人吗?这已经困扰我很长一段时间了,我需要让它休息......

提前致谢 JQ科雷亚

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

我们的影子 2024-11-07 23:58:40

在 Activity 的 onCreate 中,调用 getIntent()。如果 Activity 从启动器(主屏幕)启动,则 getAction() 的值将为 android.intent.action.MAINgetCategories()< /code> 将返回一个包含 android.intent.category.LAUNCHER 类别的集合。
如果活动是从其他地方启动的,这些值可能为 null

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.

甜味超标? 2024-11-07 23:58:40

除了 @advantej 的答案之外,您还可以将每个启动调用扩展到该活动,为启动意图添加额外的内容(例如 intent.putExtra("caller", this.getClass().getSimpleName() );

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

如果启动器不是主屏幕图标,您可以进一步检查是否有 intent.hasExtra("caller" ") 返回 true,如果是,它是什么。

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.

骄傲 2024-11-07 23:58:40

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

步骤 1:

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

步骤 2:

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

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
誰ツ都不明白 2024-11-07 23:58:40

在 2 种情况下 onRestart();称为,1.当活动来自后台时,2.当用户通过后退按钮到达活动时,示例解决方案:
使用 onBackPressed() 函数设置一个标志..所以你知道 onRestart 被调用是因为按下后退按钮...
在 onRestart() 中检查标志并重置它......

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

命比纸薄 2024-11-07 23:58:40

根据 advantej 的回答,这里是一个完整的示例,如果 Activity 是从启动器图标启动的,它也会隐藏 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);
    }

}

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

}
日记撕了你也走了 2024-11-07 23:58:40

这是方便的方法,因此您无需自己编写:

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

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;
}
春花秋月 2024-11-07 23:58:40

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

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

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

    ....

}

从其他 Activity 调用 MainActivity 时,请使用以下代码:

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

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文