public class CheckActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_check); FragmentManager manager = getSupportFragmentManager(); FragmentTransaction transaction =manager.beginTransaction(); CheckFragment fragment = new CheckFragment(); transaction.add(R.id.frame,fragment); transaction.commit(); }}public class CheckFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_check,container,false); final Button mButton = (Button) view.findViewById(R.id.button9); mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(getActivity(),NextActivity.class)); } }); return view; }}<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_check" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="mycompany.com.saveme.activity.CheckActivity"><FrameLayout android:id="@+id/frame" android:layout_width="match_parent" android:layout_height="match_parent"></FrameLayout></RelativeLayout><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"><Button android:text="Go to activity" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/button9" /></LinearLayout>Output:
First screen have 1 button, when it is clicked navigated to another screen, in second screen, we have one button, when button is pressed,this activity itself finished.