如何根据调用的Intent恢复同一个Activity中的内容?

发布于 2024-09-02 07:24:12 字数 1874 浏览 8 评论 0原文

这是我的场景:我通过 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_idtitledescr 我创建了某种信息菜单对于用户:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清欢 2024-09-09 07:24:12

onStop()onPause() 方法上专门终止 Activity(通过调用 this.finish())怎么样?当用户单击小部件的不同按钮时,这可能会产生额外的开销,但这是强制每次单击调用 onCreate() 的唯一方法。

How about specifically killing the Activity (by calling this.finish()) on your onStop() or onPause() methods? This may create extra overhead as the users click on the different buttons of your widget but is the only way you can force onCreate() to be called each click.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文