我有一个应用程序,在许多不同的活动之间。 一项活动是我的主要活动,它的行为取决于应用内的哪项活动启动了主要活动。 我认为我可以通过让所有其他活动将一个额外的名为“launcehdFrom”的意图记录下来,其中包含一个字符串,其名称为hte活动,称为主要活动。 我遇到的问题是,一旦设置了该值,就不能被其他活动覆盖。 我没有找到一个很好的直接方法来做到这一点。 任何建议?
以下代码在我的主要活动中由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(); }I have an app that goes between many different activities. One activity, my main activity, has it's behavior dependent on which activity, within the app, launched the main activity. I thought i could log this by having every other activity put an extra called "launcehdFrom" into the intent, which contains a string that has the name of hte activity that called the main activity. The problem i ran into is that once that value has been set it can't be overriden by another activity. I havne't found a good straightforward method for doing this. Any advice??
The following code is called from onResume() in my main activity:
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); }This is the code that launches the main activity the first time:
public void launchMainActivity(Obj myObj){ Intent launchMain = new Intent(this, mainActivity.class); login.putExtra("launchedFrom", ProcessIntentRequestType.startupActivity); startActivity(launchMain); }This is the code that launches the main activity from some other activity after it has been loaded atleast once by the startup activity:
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(); }最满意答案
在调用具有一些显着行为的startActivity时,您正在使用FLAG_ACTIVITY_SINGLE_TOP。 在这种情况下,重写onNewIntent并从那里调用setIntent 。 之后,该平台将调用onResume并且您对getIntent的调用将返回新的意图。
You are using FLAG_ACTIVITY_SINGLE_TOP when calling startActivity which has some notable behavior. In this case, override onNewIntent and call setIntent from there. After that, the platform will call onResume and your call to getIntent will return the new intent.
更多推荐
发布评论