Android:将附加内容更改为意图(Android: Changing extras to an intent)

编程入门 行业动态 更新时间:2024-10-28 16:27:19
Android:将附加内容更改为意图(Android: Changing extras to an intent)

我有一个应用程序,在许多不同的活动之间。 一项活动是我的主要活动,它的行为取决于应用内的哪项活动启动了主要活动。 我认为我可以通过让所有其他活动将一个额外的名为“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.

更多推荐

本文发布于:2023-08-04 23:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1424652.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:意图   内容   Android   intent   extras

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!