0

我有這段代碼在Activity中打開片段。我想要做的就是調用名爲「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); 

與片段活動我有這樣的代碼

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(); 
     } 
    } 
} 

但是,束始終爲空。這裏有什麼問題? 謝謝

回答

0

試試這個:

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(); 
      } 
     }} 
+0

它沒有按」 t工作,捆綁仍然是空:( –

+0

@SugiAlmantara什麼捆綁包?沒有捆綁了。只有一個字符串內部意圖對象 –

+0

我的意思是'getIntent()。getStringExtra(「FragmentBooking」)!= null'仍然爲null Im sorry –

0

從活動打電話給你的片段,我這是在普通類中的方法。

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(); 
    } 

從活動中傳遞包含傳遞包的片段。 (寫的onCreate代碼())

Classname classname = new Classname(); 
Bundle bundle = new Bundle(); 
bundle.putBundle("key", "value"); 
classname.setArguments(bundle); 
showFragment(classname, fragment_name, true); 

在片段

Bundle mBundle = getArguments(); 
    if (mBundle != null) { 
     String variable = mBundle.getString("key"); 
    } 

如果你不想通過捆綁的片段

Classname classname = new Classname(); 
showFragment(classname, fragment_name, true);