views:

35

answers:

1

Hi everyone,

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

+1  A: 

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.

Ricardo Villamil
I've been thinking about that. What do recommend on how to persist the data for each different kind of 'entry' (i.e. the AppWidget Buttons)? Creating a dedicated SQLite-Table?
steff
Persisting to SQLite would probably be worse than recreating your activity since at that point you're doing I/O. Think about use cases, if your users would not be doing a lot of back and forth to click the different buttons then recreating the class sounds OK. Also, if you keep your activity class tiny (not a lot of views or objects), recreating it will not be that bad either. On the other hand, I'd Look at Preferences files too, a lot less overhead than SQLite and perfect to persist simple data: http://developer.android.com/guide/topics/data/data-storage.html#pref
Ricardo Villamil
Since I'll only store very few items in the db I decide to go with it. Performance is not (truly) criticial in my app so this works out fine for me. Thanks for your help
steff
Now here's one more thing. Almost everything is working as desired. The only problem remains if one presses the home-button. I don't stand a chance to intercept its behaviour. So how can I call this.finish() if the home button is pressed?
steff
When that happens I believe android will cal onPause() or onStop() on your activity, so just override those and call finish() inside them.
Ricardo Villamil