views:

418

answers:

2

Hi everyone,

I am sure this will have been answered but I proved unable to find it. So please excuse my redundancy.

What I am trying to do is emulating the "Home" button which takes one back to Android's homescreen. So here is what causes me problems:

  • I have 3 launcher activities. The first one (which is connected to the homescreen icon) is just a (password protected) configuration activity. It will not be used by the user (just admin)
  • One of the other 2 (both accessed via an app widget) is a questionnaire app. I'm allowing to jump back between questions via the Back button or a GUI back button as well. When the questionnaire is finished I sum up the answers given and provide a "Finish" button which should take the user back to the home screen.

For the questionnaire app I use a single activity (called ItemActivity) which calls itself (is that recursion as well when using intents?) to jump from one question to another:

Questionnaire.serializeToXML();  
Intent i = new Intent().setClass(c, ItemActivity.class);  
if(Questionnaire.instance.getCurrentItemNo() == Questionnaire.instance.getAmountOfItems()) {  
    Questionnaire.instance.setCompleted(true);  
} else Questionnaire.instance.nextItem();  
startActivity(i);

The final screen shows something like "Thank you for participating" as well as the formerly described button which should take one back to the homescreen. But I don't really get how to exit the Activity properly. I've e.g. used this.finish(); but this strangely brings up the "Thank you" screen again. So how can I just exit by jumping back to the homescreen??

Sorry for the inconvinience.
Regards,
Steff

A: 

It sounds like what's happening is that you've got multiple copies of your activities open at the same time. Every time you start a new instance of an activity you're just adding new ones to the stack - all the old activities are still there. When you call this.finish(), it's just showing you the next most recent activity that was open. I'm not sure why that's the "Thank you" screen since presumably that only gets opened at the end, but then I don't fully follow the sequence your activities get called in.

What you can try doing to fix it is either: 1) use a finish() every time after starting a new activity for a different question (so that there's only ever one question activity open at a time), or 2) see if you can make use of the intent flag CLEAR_TOP. That starts the target activity while killing anything that might have been above it in the stack. For example if you have activities: A, B, C, C, C, C, C (etc for each question), then starting A with CLEAR_TOP will kill off all instances of B and C. You could fit this into your program structure by changing the first screen the user sees when they start a questionnaire. This would show a 'welcome' message by default with a button to start the questionnaire. However, if it's started with a particular value bundled with the intent, it would display the "Thank you" message.

If you do that, then once you finish() activity A, the user will be returned to wherever they were before they started your app - presumably the homescreen if it's started with an appwidget.

Steve H
Thanks, I'll give it a go. I After reading your answer I was wondering if I followed an entirely wrong approach of making a questionnaire...
steff
A: 

Bonjour Steff, pour convenir à tout le monde je vais continuer la suite en Anglais

I just read this article by Reto Meier http://blog.radioactiveyak.com/2010/05/when-to-include-exit-button-in-android.html

When a user wants to exit the application, the best way is to press Home . Activity.finish() is exactly equivalent to hitting back button. The services keep running and the ressources aren't released.

raychenon