简体   繁体   中英

Android Studio Back Button Problems

For so far. I have created a simple quiz application on Android Studio. Everything works fine, including when I go from the FirstActivity.java to the Next Activity, which is named SecondActivity.java, and close the first activity with finish(),when the button is pressed, as shown in the following code:

 public void onClick () {   

    button_next = (Button)findViewById(R.id.nextbtn);


            button_next.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(FirstActivity.this, SecondActivity.class));
                    finish();


                }
            });

The code works great But when I try to go from the Second Activity to the First Activity (Closing the Second Activity and going back to the first Activity), neither finish() nor onBackPressed() is working, it just closes the application completely, what code do I need to as soon as I press the button, close the SecondActivity.class and go to the FirstActivity.class?

If you want to go back to previous activity, dont put finish after you change your activity.

public void onClick() {
    button_next = (Button) findViewById(R.id.nextbtn);
    button_next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(FirstActivity.this, SecondActivity.class));
        }
    });
}

If you have so many activities ,there is another way handle activity life cycle:

public class MyActivityManager {
    private static MyActivityManager instance;
    private Stack<Activity> activityStack;//activity's stack
    private MyActivityManager() {
    }
    //singleton
    public static MyActivityManager getInstance() {
        if (instance == null) {
            instance = new MyActivityManager();
        }
        return instance;
    }
    //push an activity to stack
    public void pushOneActivity(Activity actvity) {
        if (activityStack == null) {
            activityStack = new Stack<Activity>();
        }
        activityStack.add(actvity);
    }
    //get last activity,fifo
    public Activity getLastActivity() {
        return activityStack.lastElement();
    }
    //remove an activity
    public void popOneActivity(Activity activity) {
        if (activityStack != null && activityStack.size() > 0) {
            if (activity != null) {
                activity.finish();
                activityStack.remove(activity);
                activity = null;
            }
        }
    }
    //finish all activity
    public void finishAllActivity() {
        if (activityStack != null) {
            while (activityStack.size() > 0) {
                Activity activity = getLastActivity();
                if (activity == null) break;
                popOneActivity(activity);
            }
        }
    }
}

at the FirstActivity,you shall add

MyActivityManager.getInstance().pushOneActivity(FirstActivity.this);

at the SecondActivity, you want to go FirstActivity or other activity:

startActivity(new Intent(FirstActivity.this, SecondActivity.class));
MyActivityManager.getInstance().finishAllActivity();
finish();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM