Bụng Bự
Bụng Bự

Reputation: 553

getArgument get nullexception when fragment create view 2nd time

By default, the listview fragment will be shown 1st on my activity

Here is my activity:

public class SecondActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        FragmentManager manager = getFragmentManager();
        FragmentTransaction tran = manager.beginTransaction();
        ListViewFragment fr = new ListViewFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelable("Object", getIntent().getParcelableExtra("ObjectFromCategory"));
        fr.setArguments(bundle);
        tran.addToBackStack(null);
        tran.replace(R.id.places_fragment_cointainer, fr, "ListView");
        tran.commit();
        //handleFragmentCycle();
    }

Then in ListviewFragment I get argument:

public class ListViewFragment extends Fragment implements OnClickListener {
    private TextView mTxtMapView;
    private Category mObject;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View v = inflater.inflate(R.layout.fragment_places_listview, container,false);
        mTxtMapView = (TextView)v.findViewById(R.id.txt_mapview);
        mObject = getArguments().getParcelable("Object");
        Toast.makeText(getActivity(), mObject.getText(), Toast.LENGTH_LONG).show();
        mTxtMapView.setOnClickListener(this);
        return v;
    }

and here is my MapViewFragment:

public class MapViewFragment extends Fragment implements OnClickListener {

    TextView txtListView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View v = inflater.inflate(R.layout.fragment_places_mapview, container, false);
        FragmentManager fm = getChildFragmentManager();
        MapFragment supportMapFragment = MapFragment.newInstance();
        fm.beginTransaction().replace(R.id.mapwhere, supportMapFragment).commit();
        txtListView = (TextView) v.findViewById(R.id.txt_listview);
        txtListView.setOnClickListener(this);
        return v;

    }

At first time ListViewFragment is created, I can get the argument successfully, but then I get null when I change to MapViewFragment then change back to ListViewFragment. I figure out a solution that I should pass an int value in bundle to detect if ListViewFragment is created from Activity or MapViewFragment. But I dont think it is a good solution cuz I still want to get the Category Objecct,so I want to hear if anybody have a better solution for this case. Thank you!

Upvotes: 2

Views: 92

Answers (1)

Linh
Linh

Reputation: 60973

You can store your object to SharePreference using gjson in your Activity.
Then in Fragment, you retrieve your object from SharePreference. After that, you will never get null object in your Fragment

===
This is the way to save and retrieve the object from SharePreference
Creating

SharedPreferences  mPrefs = getPreferences(MODE_PRIVATE);//Creating shared preference

For save object

Editor prefsEditor = mPrefs.edit();
Gson gson = new Gson();
String json = gson.toJson(myObject); // myObject - instance of MyObject
prefsEditor.putString("Object", json);
prefsEditor.commit();

For retrieve object

Gson gson = new Gson();
String json = mPrefs.getString("Object", "");
MyObject obj = gson.fromJson(json, MyObject.class);

Upvotes: 1

Related Questions