我有一个应用程序,包含许多不同的活动。其中一个活动是我的主要活动,它的行为取决于启动主要活动的活动。我想通过让每个其他活动在意图中添加名为“launchedFrom”的额外调用来记录此状态,其中包含调用主要活动的活动名称字符串。但我遇到的问题是,一旦该值已经设置,就无法被另一个活动覆盖。我还没有找到很好的简单方法来解决这个问题。是否有任何建议?
以下代码从我的主要活动的onResume()中调用:
这是在启动活动至少加载一次后,从其他活动中启动主要活动的代码:
以下代码从我的主要活动的onResume()中调用:
private void processIntentRequest(){
Intent intent = getIntent();
ProcessIntentRequestType caller = (ProcessIntentRequestType)intent.getSerializableExtra("launchedFrom");
switch(caller){
case startUpActivity:
load(this.myObj);
break;
case otherActivity:
int uri = integer.parseInt(this.getIntent().getExtras().getString("uri"));
load(this.myObj, uri);
break;
default:
load(this.myObj, 1);
}
这是第一次启动主活动的代码:
public void launchMainActivity(Obj myObj){
Intent launchMain = new Intent(this, mainActivity.class);
login.putExtra("launchedFrom", ProcessIntentRequestType.startupActivity);
startActivity(launchMain);
}
这是在启动活动至少加载一次后,从其他活动中启动主要活动的代码:
protected void launchMainActivity(Obj myObj, HelperObj helper) {
String uri = helper.uri;
Intent mainActivity = new Intent(this, MainActivity.class);
mainActivity.putExtra("uri", uri);
mainActivityputExtra("launchedFrom", ProcessIntentRequestType.otherActivity);
startActivity(mainActivity.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT));
finish();
}