19

While the application is running, I press the HOME button to close the application. When I start the application again, it resumes on the page displayed prior to clicking on HOME. I want the application to start with the initial display instead. I have used finish() to finish the activity but it is not working. Any suggestions?

Marc Mutz - mmutz
  • 24,485
  • 12
  • 80
  • 90
Nikki
  • 3,314
  • 13
  • 36
  • 59
  • 1
    where did you place the 'finish()'? In most case, pressing home only puts the activity to pause unless Android needs that piece of memory urgently. – xandy Nov 09 '10 at 08:15
  • You didn't explain us where you used finish(). – cababunga Nov 09 '10 at 08:20

3 Answers3

22

Most likely you have several instances of the same activity. To resolve this kind of issues create your own parent Activity class e.g. MyRootActivity which will hold static list of all of available/alive activities:

public class MyRootActivity extends Activity
{
    private static final String TAG=MyRootActivity.class.getName();
    private static ArrayList<Activity> activities=new ArrayList<Activity>();


    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        activities.add(this);
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        activities.remove(this);
    }

    public static void finishAll()
    {
        for(Activity activity:activities)
           activity.finish();
    }
}

For that all of your activities need to be children of MyRootActivity.

Then when you are about to sure that you're closing your application - just call MyRootActivity.finishAll();

Barmaley
  • 16,638
  • 18
  • 73
  • 146
0

Create a static Activity object which activity finish on other activity and assign activity in this i.e you can can add more activities

public class demoActivity extends AppCompatActivity {
    public static Activity self_intent;
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.demo_activity);
            selfintent=this;
    } 

   //Other functions--------------
} 

do same for other activities

on other

public class finishingActivity extends AppCompatActivity {
        public Button activityCloseBtn;
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.finishing_activity);

            activityCloseBtn= (Button) view.findViewById(R.id.activity_close_btn);
            activityCloseBtn.setOnClickListener(new View.OnClickListener() {      
            @Override
            public void onClick(View v) {
               demoActivity.selfintent.finish(); //for finish demoActivityactivity

              //for other activities Activity.selfintent.finish();
               finish();  //for finish current activity
          }
    });
-2

try calling super.onPause() first and later call finish() inside your onPause() stub

RinoTom
  • 2,278
  • 2
  • 26
  • 40