views:

752

answers:

4

I have 3 activities. Activity A which leads to activity B, which in turn can go back to activity A or start activity C. However, if I press back in activity C the app should close.

To sum up:

  • Activity A starts activity B
  • Pressing Back on activity B should lead to A
  • Activity B starts activity C
  • Pressing Back on activity C should close the app

How should I go from activity B to C? This code currently gives me a NullPointerException on the last line:

Intent intent=new Intent(ActivityB.this, ActivityC.class);
startActivity(intent);
ActivityB.this.finish();
ActivityB.this.getParent().finish();

If I switch the last two lines I also get a null pointer.

A: 

Have each activity listen for a result from the next activity, and finish itself if that occurs.

You can use a special result code to indicate that you want the activity to finish.

Mayra
+2  A: 

I don't know if this will work, but you could try it:

  • From Activity A, start activity B for a result using startActivityForResult()

  • In Activity B, when the user triggers Activity C, start activity C.

startActivity() returns immediately, so

  • set a result that will inform A to finish as well,

  • Call finish() in B.

  • When A receives that result from B, A calls finish() on itself as well.

Failing that, you could make Activity C into its own app and then close the first app (with A & B) after it starts the second.

P.S. Take Falmarri's comment into consideration as you move forward!
Good luck.

codinguser
A: 

You shou use onActivityResult method in your parent Activity

Suppose Activity A is parent of Activity B if you want to click back button in Activity B to exit Application (also exit Activity A)

In your Activity B, in onStop() or onDestory()

you call

setResult(0); //any int number is fine

this will pass a result code to its parent activity

Your parent Actvity A, listens for the result code you will need to use onActivityResult method inside the method you can call

if(resultCode == 0) //matches the result code passed from B
{
    ActivityA.this.finish()
}

It works for me :)

TS.xy
A: 

Hi,

I am trying to develop a Remote Test tool application which runs in background under Android 1.5+ SDK Emulator version Environment.

The purpose of this application is to capture the screen shots on Real Physical Device OR Emulator and store in SDCARD, whenever User browses from one screen to another, using this application during running phase.

If you have related info / code package for the same, please send it. Thanks.

I appreciate your valuable feedback/support and oblige.

Looking forward to hear from you soon, Thanks, Narasimha ([email protected])

Narasimha
@Narashimha: Please start your own question
Casebash