簡體   English   中英

從活動到碎片均可打包

[英]Parcelable from activity to fragment

我是可打包的新手,我正在嘗試將數據從活動(MainActivity)傳遞到片段(MainFragment),但是我很難做到這一點。

我使用所有(可打包)數據制作了一個類(InfoBean)。 當我從MainActivity發送數據時 ,來自bean.newTheme( 2131296447 )的數據就在那里,但是當我嘗試在Fragment中檢索時 ,該值為0

有人可以看看我在做什么錯嗎? 謝謝您的幫助。

發送數據(MainActivity):

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    InfoBean bean = new InfoBean();

  @Override
  protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  SecureSharedPreferences theme = SecureSharedPreferences.getInstance(this, "MyPrefsFile");

        int newTheme = theme.getInt("themeCustom", 0);
        bean.newTheme = newTheme;

        Bundle bundle = new Bundle();
        bundle.putInt("theme", bean.newTheme); // debug shows value 2131296447 
        MainFragment mf = new MainFragment();
        mf.setArguments(bundle); 
    //
  }
}

檢索數據(MainFragment):

public class MainFragment extends Fragment {

  InfoBean bean = new InfoBean();

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

   //
   Bundle bundle = this.getArguments(); // Debugging shows 0!
     if (bundle != null) {
         bean.newTheme = bundle.getInt("theme");
        }

     if (bean.newTheme == 2131296447) { // White Theme
         mCardView1.setBackgroundColor(Color.parseColor("#E8EBED"));
        } else { // Dark Theme
         mCardView1.setBackgroundColor(Color.parseColor("#282929"));
         relLay.setBackgroundColor(Color.parseColor("#1B1C1C"));
        }

        return rootView;
    }
}

InfoBean.class:

public class InfoBean implements Parcelable {

    public int newTheme;
    public int THEME_DARK = R.style.DarkTheme;
    public int THEME_LIGHT = R.style.LightTheme;

@Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.newTheme);
        dest.writeInt(this.THEME_DARK);
        dest.writeInt(this.THEME_LIGHT);

}

    public InfoBean() {
    }

    protected InfoBean(Parcel in) {
        this.newTheme = in.readInt();
        this.THEME_DARK = in.readInt();
        this.THEME_LIGHT = in.readInt();

}

    public static final Parcelable.Creator<InfoBean> CREATOR = new Parcelable.Creator<InfoBean>() {
        @Override
        public InfoBean createFromParcel(Parcel source) {
            return new InfoBean(source);
        }

        @Override
        public InfoBean[] newArray(int size) {
            return new InfoBean[size];
        }
    };
}

更新

由於您將片段嵌入到xml中,因此無法將值傳遞給片段類。 為此,您需要使其通過Java代碼執行並刪除該xml。 進行片段交易,然后它將起作用

更新

您應該嘗試在片段的onCreate方法中檢索值。

@overide
protect void onCreate(bundle onSavedInstance){
   if (savedInstance != null) {
     bean.newTheme = bundle.getInt("theme");
    }
}

嘗試這個

 if (bundle != null) {
         bean.newTheme = bundle.getInt("theme");
        }

代替

 if (bundle != null) {
         bean.newTheme = bundle.getParcelable("theme");
        }

在您的活動中,您正在使用.putInt(“ theme” .....),但在片段中,您將調用.getParcelable(“ theme”)。 由於要嘗試獲取兩種不同的數據類型,因此得到0。

如果您在XML中嵌入了片段,則無法在程序中使用setArguments() 最好使用動態片段創建。

android開發者網站上有一個簡短的示例可以指導您: http : //developer.android.com/reference/android/app/Fragment.html當您嵌入了片段以及如何使用該片段處理參數時,還有另一種實現。

這里還有另一種資源可以幫助您:

從活動設置片段的參數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM