How to restore content in the same Activity based on the calling Intent?
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.
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,
SteffHow 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.
精彩评论