1

close the total application will closed when we click on home in any activity and again start the application from starting onwards (Starting Activity).In my application activities contain back button also.For this Give me some suggestions.

Thanks in advance

ingsaurabh
  • 15,249
  • 7
  • 52
  • 81
Pinki
  • 21,723
  • 16
  • 55
  • 88
  • 1
    You should re-write you question to make it more understandable. I personally didn't understand what you have and what you want. – inazaruk Jun 02 '11 at 08:19
  • How is a user supposed to exit your application if pressing home always restarts it? – Ben Williams Jun 02 '11 at 08:24
  • I think he means when someone presses home, quit back to the original activity, then when it next starts start from this original activity again – Blundell Jun 02 '11 at 08:40

1 Answers1

1

Have an activity that extends application, in here you need two flag's.

Declaring Global Variables

  public class MyApplication extends Application {
         public static boolean flagForHome= false;
         public static boolean flagForChangingActivity= false;

  }

Then in onPause and onResume of each activity

Activity Lifecycle

    public void onResume(){                  
            if(flagForHome && !flagForChangingActivity){
                finish();
            } else {
                MyApplication.flagForHome= false;
                MyApplication.flagForChangingActivity= false;
            }
    }

    public void onPause(){                  
            MyApplication.flagForHome= true;
    }

Finally when changing activity normally using startActivity(); you will need to set the flagForChangingActivity to true;

Using Intents

   MyApplication.flagForChangingActivity= true;       
   Intent intent = new Intent(this, ActivityTwo.class);
   startActivity(intent);

this will stop your app closing when changing activity (as the activity going into the background will hit onPause and the new activity starting up will hit onResume)

Don't forget in your home activity to set the flag back to false. This will stop your app instantly closing itself when you start it the second time!

HomeActivity.class:

  public void onPause(){                  
            Application.flagForHome= false;
    }

EDIT

Start Activity for Result is just the same procedure:

 MyApplication.flagForChangingActivity= true;       
 Intent intent = new Intent(this, ActivityTwo.class);
 startActivityForResult(intent, 0);
Community
  • 1
  • 1
Blundell
  • 75,855
  • 30
  • 208
  • 233