End current activity from fragment

I have a snippet in activity that I use as a navigation box. It contains buttons that, when pressed, start new actions (startActivity from a fragment simply calls startActivity for the current activity).

In my life, I can’t understand how to finish the current activity after starting a new one.

I want to achieve something similar in the fragment:

@Override public void onClick(View view) { // TODO Auto-generated method stub if (view == mButtonShows) { Intent intent = new Intent(view.getContext(), MyNewActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); finish(); } } 

But it seems that Fragment.class does not implement finish () (for example, it implements startActivity (...)).

I would like the backstack activity to clear when the 2nd action starts. (so rolling back from a new activity will technically drop them back to the launcher)

+81
android
Oct 26 '11 at 19:37
source share
8 answers

When working with fragments, instead of using this or a context reference, always use getActivity() . You have to call

 getActivity().finish(); 

to complete your activity from fragment.

+239
Oct 26 '11 at 19:41
source share

Well actually ...

I would not want Fragment to try to end the Activity. In my opinion, this affects the Fragment too much. Instead, I would use the guide here: http://developer.android.com/training/basics/fragments/communicating.html

Define the Fragment interface that the Activity should implement. Make a call for the Activity, then let the Activity decide what to do with the information. If the activity wants to end itself, then it can.

+23
Aug 28 '14 at 16:03
source share

As Jon F Hancock mentioned, this is how a fragment can β€œclose” an activity by proposing to close this activity. This makes fragment transfer a reason for them. If you use it in another action, you may not want to close the activity.

Below is a snippet of action and a snippet that has a save and cancel button.

PlayerActivity

 public class PlayerActivity extends Activity implements PlayerInfo.PlayerAddListener { public void onPlayerCancel() { // Decide if its suitable to close the activity, //eg is an edit being done in one of the other fragments? finish(); } } 

PlayerInfoFragment, which contains the interface that the call implementation should implement.

 public class PlayerInfoFragment extends Fragment { private PlayerAddListener callback; // implemented in the Activity @Override public void onAttach(Activity activity) { super.onAttach(activity); callback= (PlayerAddListener) activity; } public interface PlayerAddListener { public void onPlayerSave(Player p); // not shown in impl above public void onPlayerCancel(); } public void btnCancel(View v) { callback.onPlayerCancel(); // the activity implementation } } 
+6
Nov 01 '14 at 2:14 on
source share

Very simple...

1- just grab the getActivity() activity in the fragment

2- then call finish();

So just getActivity().finish(); complete parental activity.

+2
Jul 28 '17 at 10:32
source share

Every time I use the finish line to close a fragment, all activity is closed. According to the documents, the fragments should remain as long as the parental activity persists.

Instead, I found that I could change the views back to parent activity using this statement: setContentView (R.layout.activity_main);

This brings me back to parental activity.

I hope this helps someone else who can look for it.

+1
Sep 12 '15 at 23:27
source share

You must use the getActivity () method to complete the action from the fragment.

 getActivity().finish(); 
+1
Aug 6 '17 at 15:12
source share

try to use this

yes Fragment.class does not implement finish()

When working with fragments, instead of using this or a context reference, always use getActivity() . You have to call

0
Nov 03 '15 at 11:32
source share

In fragment use getActivity.finishAffinity ()

0
Jul 01 '19 at 9:39
source share



All Articles