I have this code to open fragment in Activity. what i want to do is to call a fragment with name "book".
Intent intent = new Intent(this, Dashboard_Admin.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.putExtra("FragmentBooking", "book");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
In activity with a fragment i have this code
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard_admin);
Bundle extra = getIntent().getExtras();
Toast.makeText(this, "test!!!!!!!!!!!!!!!!!!!!", Toast.LENGTH_SHORT).show();
if (extra!=null){
String idFrg = extra.getString("FragmentBooking");
Toast.makeText(this, "Hello: "+idFrg, Toast.LENGTH_SHORT).show();
if (idFrg.equals("book")){
Log.d(TAG_SUCCESS, "Test");
Fragment fragment = null;
Class fragmentClass = null;
fragmentClass = FragmentBookingKendaraan.class;
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContentAdmin, fragment).commit();
}
}
}
But, the bundle is always null. What's wrong here ? Thank you
Try this :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard_admin);
if (getIntent().getStringExtra("FragmentBooking")!=null){
if (getIntent().getStringExtra("FragmentBooking").equals("book")){
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager
.beginTransaction()
.replace(R.id.flContentAdmin, new FragmentBookingKendaraan())
.commit();
}
}}
To call your fragment from Activity, I have a method which is in common class.
public void showFragment(Fragment fragment, String back_stack_name, boolean isAddToBackStack) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(R.id.container, fragment);
if (isAddToBackStack) {
transaction.addToBackStack(back_stack_name);
}
transaction.commit();
}
call fragment from Activity with passing bundles. (Write this code in onCreate())
Classname classname = new Classname();
Bundle bundle = new Bundle();
bundle.putBundle("key", "value");
classname.setArguments(bundle);
showFragment(classname, fragment_name, true);
In fragment
Bundle mBundle = getArguments();
if (mBundle != null) {
String variable = mBundle.getString("key");
}
If you don't want to pass bundle to the fragment
Classname classname = new Classname();
showFragment(classname, fragment_name, true);
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.