我需要知道一种通用方法来区分来自启动器的活动调用和来自我的应用程序内部的另一个活动的调用,或活动堆栈上的 BACK
任何人?这困扰了我很长一段时间,我需要让它休息...
在此先感谢 JQCorreia
我需要知道一种通用方法来区分来自启动器的活动调用和来自我的应用程序内部的另一个活动的调用,或活动堆栈上的 BACK
任何人?这困扰了我很长一段时间,我需要让它休息...
在此先感谢 JQCorreia
在 Activity 的 onCreate 中,调用getIntent()
. 如果 Activity 是从启动器(主屏幕)启动的,则 的值getAction()
将是android.intent.action.MAIN
并且getCategories()
将返回一个包含 android.intent.category.LAUNCHER 类别的集合。如果活动从其他地方开始,这些值可能是null
.
除了@advantej 的回答之外,您还可以将每个启动调用扩展到该活动,为启动意图添加额外的内容(例如intent.putExtra("caller", this.getClass().getSimpleName()
);
在活动的onCreate
方法中,您可以检查@advantej 的建议。
如果启动器不是主屏幕图标,那么您可以进一步检查是否intent.hasExtra("caller")
返回 true,如果是,它是什么。
您可以从意图标志中找到它。
步骤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
在 2 种情况下 onRestart(); 调用,1.当活动来自后台时,2.当用户通过后退按钮到达活动然后示例解决方案:使用 onBackPressed() 函数设置一个标志..所以你知道 onRestart 调用是因为按下后退按钮......在 onRestart() 检查标志并重置它,然后......
根据advantej 的回答,这是一个完整的示例,如果活动是从启动器图标启动的,它也会隐藏 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);
}
}
这是方便的方法,因此您无需自己编写:
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;
}
我能想到的最简单的方法是在从您自己的活动中启动活动时传递一个标志。您还应该检查活动是否已创建或恢复,这可以通过在 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);
}