how to intent with on click button from activity to fragment in android studio. please anyone tell me what is exact code to intent from activity to fragment with button click on activity.
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button mButNext = (Button) findViewById(R.id.btn_next);
mButNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(this, PropertyListFragment.class);
startActivity(i);
}
});
}
}
PropertyListFragment.class
public class PropertyListFragment extends Fragment {
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public static PropertyListFragment newInstance(String param1, String param2) {
PropertyListFragment fragment = new PropertyListFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
public PropertyListFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
String strtext = getArguments().getString("pincode");
return inflater.inflate(R.layout.fragment_property_list, container, false);
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
public void onFragmentInteraction(Uri uri);
}
}
fragment_property_list.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@+id/sample_content_fragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
Button mButNext = (Button) findViewById(R.id.btn_next);
mButNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
PropertyListFragment plf = PropertyListFragment.newInstance("param1", "param2");
getSupportFragmentManager().beginTransaction().replace(R.id.sample_content_fragment, plf, "tag").commit();
}
});
You use an Intent to open another Activity.
If your goal is to show the Fragment in it's own Activity then create an intent to start the new Activity, and have the target Activity load your Fragment in onCreate. See Adding a Fragment to an Activity
If your goal is to show the Fragment in the same Activity then read slightly further down in the previous link about programmatically add the fragment to an existing ViewGroup
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.