简体   繁体   中英

Android - How to back to the second fragment on Activity 1 from Activity 2?

I have three fragments on my activity1.

[1], [2], [3]

And Fragment[2] lists some items, when I click specific item, it will start activity2.

There is one back button on activity2. When pressing it, how do I return fragment[2] on activity1?

What is the best practise to do that?

Use the code in the second activity:

  Button backbtn=(Button)findViewById(R.id.back);

    backbtn.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View view)
        {
            finish();
        }
    });

You normally don't create "back buttons". Therefore you use the already implemented "back button" from android.
But if you still want to go back you use "finish()" or "system.exit(0)" (like "Danny" showed you!).

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