如何根据调用的Intent恢复同一个Activity中的内容?
这是我的场景:我通过 AppWidget 启动 Activity A。 AppWidget 显示(除其他外)3 个按钮。他们每个人都有自己的意图。它们旨在通过名为 AppWidgetReceiver 的类向 Activity 提供信息。在后者中,我创建了一个这样的意图:
Intent i = new Intent(context, CreateNoteActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra(AppWidget.WIDGET_ITS_TRIGGER_ID, trigger_id);
i.putExtra(AppWidget.WIDGET_TITLE, title);
i.putExtra(AppWidget.WIDGET_DESCRIPTION, descr);
context.startActivity(i);
这将启动活动 A。基于 trigger_id
、title
和 descr
我创建了某种信息菜单对于用户:
coreQuestionTitle = getIntent().getStringExtra(AppWidget.WIDGET_TITLE);
coreQuestionDescr = getIntent().getStringExtra(AppWidget.WIDGET_DESCRIPTION);
coreQuestionId = getIntent().getIntExtra(AppWidget.WIDGET_ITS_TRIGGER_ID, -1);
TextView tvCoreQuestion = (TextView) findViewById(R.id.create_note_core_question_text);
tvCoreQuestion.setText(coreQuestionTitle);
所有这些都运行良好。当我不通过 this.finish()
“关闭”活动(例如按下主页按钮或后退按钮)时,就会出现问题。现在,当我单击 AppWidget 上的“其他”按钮时,先前打开的“活动”将恢复,而不是重新启动。未调用 onCreate()
。相反,它直接跳转到 onStart()
。我知道这是活动生命周期的目的,因为任务仍然存在。
不幸的是,我确实需要能够检索意图中指定的参数来设置所需的信息。此外,我希望能够恢复任何 3 个不同任务的状态。谁能帮我解决这个问题吗?
顺便说一句:我通过使用 android:configChanges="keyboardHidden|orientation" 和 onConfigurationChanged()
来避免传递 onCreate()
。 Manifest.xml 中 Activity 的声明如下所示:
<activity android:name=".notes.CreateNoteActivity"
android:configChanges="keyboardHidden|orientation"
android:label="@string/create_note"
android:launchMode="singleTask"
android:theme="@android:style/Theme.NoTitleBar" >
<intent-filter>
<category android:name="android.intent.category.CATEGORY_HOME" />
</intent-filter>
</activity>
提前致谢,
斯特夫
here's my scenario: I start an Activity A via an AppWidget. The AppWidget displays (inter alia) 3 buttons. Each of them has its own intent. They are designed to provide information to the Activity via a class called AppWidgetReceiver. In latter I create an intent like this:
Intent i = new Intent(context, CreateNoteActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra(AppWidget.WIDGET_ITS_TRIGGER_ID, trigger_id);
i.putExtra(AppWidget.WIDGET_TITLE, title);
i.putExtra(AppWidget.WIDGET_DESCRIPTION, descr);
context.startActivity(i);
This launches the Activity A. Based on trigger_id
, title
and descr
I create some sort of information menu for the user:
coreQuestionTitle = getIntent().getStringExtra(AppWidget.WIDGET_TITLE);
coreQuestionDescr = getIntent().getStringExtra(AppWidget.WIDGET_DESCRIPTION);
coreQuestionId = getIntent().getIntExtra(AppWidget.WIDGET_ITS_TRIGGER_ID, -1);
TextView tvCoreQuestion = (TextView) findViewById(R.id.create_note_core_question_text);
tvCoreQuestion.setText(coreQuestionTitle);
All of this works fine. The problem arises when I don't 'close' the Activity via this.finish()
, e.g. the home- or back-button is pressed. When I now click on an OTHER button on the AppWidget the previously opened Activity is restored instead of restarted. The onCreate()
is not called. Instead, it jumps right into onStart()
. I know that this is intended by the Activity lifecycle since the task is still alive.
Unfortunately, I really need to be able to retrieve the parameters specified in the intent to set the desired information. Moreover, I would like to be able to restore the state of ANY of 3 different tasks. Can anyone help me out on this?
By the way: I'm avoiding to pass the onCreate()
by making use of android:configChanges="keyboardHidden|orientation" and onConfigurationChanged()
. The declaration of the Activity in the Manifest.xml looks like this:
<activity android:name=".notes.CreateNoteActivity"
android:configChanges="keyboardHidden|orientation"
android:label="@string/create_note"
android:launchMode="singleTask"
android:theme="@android:style/Theme.NoTitleBar" >
<intent-filter>
<category android:name="android.intent.category.CATEGORY_HOME" />
</intent-filter>
</activity>
Thanks in advance,
Steff
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
onStop()
或onPause()
方法上专门终止 Activity(通过调用this.finish()
)怎么样?当用户单击小部件的不同按钮时,这可能会产生额外的开销,但这是强制每次单击调用onCreate()
的唯一方法。How about specifically killing the Activity (by calling
this.finish()
) on youronStop()
oronPause()
methods? This may create extra overhead as the users click on the different buttons of your widget but is the only way you can forceonCreate()
to be called each click.