Finishing current activity from a fragment

I have a fragment in an activity that I am using as a navigation drawer. It contains buttons that when clicked start new activities (startActivity from a fragment simply calls startActivity on the current activity).

For the life of me I can't seem to figure out how I would finish the current activity after starting a new one.

I am looking to achieve something like this 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 Fragment.class does not implement finish() (like it implements startActivity(...)).

I would like the activity backstack cleared when they launch the 2nd activity. (so pressing back from the new activity would technically drop them back to the launcher)

154920 次浏览

When working with fragments, instead of using this or refering to the context, always use getActivity(). You should call

爪哇咖啡

getActivity().finish();

科特林

activity.finish()

来完成你的任务。

其实..。

I wouldn't have the Fragment try to finish the Activity. That places too much authority on the Fragment in my opinion. Instead, I would use the guide here: http://developer.android.com/training/basics/fragments/communicating.html

让片段定义一个活动必须实现的接口。调用 Activity,然后让 Activity 决定如何处理这些信息。如果活动希望自己完成,那么它可以完成。

正如 乔恩 · F · 汉考克所提到的,这就是片段通过建议活动关闭来“关闭”活动的方式。这使得片段具有可移植性,这就是它们的原因。如果在其他活动中使用它,则可能不希望关闭该活动。

下面的代码是一个活动和片段的片段,其中有一个保存和取消按钮。

玩家活动

public class PlayerActivity extends Activity
implements PlayerInfo.PlayerAddListener {


public void onPlayerCancel() {
// Decide if its suitable to close the activity,
//e.g. is an edit being done in one of the other fragments?
finish();
}
}

PlayerInfoFragment,它包含调用活动需要实现的接口。

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's implementation
}
}

每次我用 Finish 关闭碎片,整个活动就会关闭。根据文档,只要父活动保持不变,片段就应该保持不变。

相反,我发现我可以通过使用以下语句来更改父活动的视图: SetContentView (R.layout.activity _ main) ;

这将我返回到父活动。

I hope that this helps someone else who may be looking for this.

try to use this

是的,Fragment.class不实现 finish()

当处理片段时,不要使用这个或引用上下文,总是使用 getActivity()

很简单。

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

2-然后呼叫 finish();

所以只有 getActivity().finish();将完成父活动。

您应该使用 getActivity ()方法来完成片段中的活动。

getActivity().finish();

In Fragment use getActivity.finishAffinity()

getActivity().finishAffinity();

It will remove all the fragment which pushed by the current activity from the Stack with the Activity too...

试试这个,不应该有任何警告..。

            Activity thisActivity = getActivity();
if (thisActivity != null) {
startActivity(new Intent(thisActivity, yourActivity.class)); // if needed
thisActivity.finish();
}

爪哇和 Kotlin 有两个选择。然而,这两种方式的逻辑是相同的。应该在调用 完成()方法之后调用 活动

科特林的回答,

如果您的活动不能为空,使用 答案 _ 1。但是,如果您的活动 可以是无效的,使用 答案 _ 2

Answer_1: activity!!.finish()
Answer_2: activity?.finish()

答案是 Java,

getActivity().finish();

这不需要断言,最新的更新片段在 android JetPack

requireActivity().finish();

Simple solution:

activity?.finish()

完成片段使用中的活动:

getActivity().finish();