frankiesardo / icepick

Android Instance State made easy

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Android save automatilcaly InstanceState not working

pishguy opened this issue · comments

I'm using this library on my project and i try to save InstanceState automatically by that, but after change phone state, i cant get data from savedInstance and put it on List as data

My custom bundler:

public class SimCardsBundler implements Bundler<SimCardLists> {
    @Override
    public void put(String s, SimCardLists example, Bundle bundle) {
        bundle.putParcelable(s, Parcels.wrap(example));
    }

    @Override
    public SimCardLists get(String s, Bundle bundle) {
        return Parcels.unwrap(bundle.getParcelable(s));
    }
} 

Simcard's Model:

@Parcel
public class SimCardLists extends RealmObject {

    private String ID;
    private String Number;
    private String Price;
    private String Date;
    private String Status;
    private String Cat;
    private String SalesName;
    private String TransID;
    private String SalesDate;
    private String SalesTime;
    private String SalesSuccess;

    public SimCardLists() {
    }

    /* SETTER and GETTES*/
}

My activity to use that:

public class ActivityMain extends AppCompatActivity {

    @State(SimCardsBundler.class)
    SimCardLists simCards;

    @BindView(R.id.simcard_lists)
    RecyclerView simcardListItems;

    @BindView(R.id.filter_by)
    TextView filter_by;

    @BindView(R.id.toolbar)
    Toolbar toolbar;

    private Context            context;
    private ActivityMain       activity;
    private Realm              realm;
    private Handler            handler;
    private List<SimCardLists> simcardLists;
    private SimCardListAdapter adapter;
    private ProgressDialog     progressDialog;
    private List<SimCardLists> simLists;

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

        ButterKnife.bind(this);

        //@formatter:off
            context                    = getBaseContext();
            activity                   = ActivityMain.this;
            realm                      = Realm.getDefaultInstance();
            handler                    = new Handler();
            simcardLists               = realm.where(SimCardLists.class).findAll();
            progressDialog             = new ProgressDialog(this);
        //@formatter:on

        realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                realm.deleteAll();
            }
        });
        SP.getInstance().setInt(SP.SharedPrefsTypes.currentRetrievePage, 0);

        List<SimCardLists> initialize = new ArrayList<>();
        simcardListItems.setLayoutManager(new LinearLayoutManager(context));
        adapter = new SimCardListAdapter(context, initialize, simcardListItems);
        simcardListItems.setAdapter(adapter);

        if (realm.where(SimCardLists.class).count() <= 0) {
            showProgressDialog();

            startService(
                    new Intent(
                            context, WebService.class)
                            .putExtra("category", 0)
                            .putExtra("request_type", "getLatestSimCardNumbers")
            );
        }

        if (savedInstanceState == null) {
            Log.e("Its null", "");
            simLists = realm.where(SimCardLists.class).findAll();
            adapter.setData(simLists);
            adapter.notifyDataSetChanged();
        }else{
            Log.e("NOT null", "");
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Icepick.saveInstanceState(this, outState);
    }

}

i read again library documentation to resolve this probelm, but i can not find other helpful tips

if your class implements Parcelable why would you make a custom Bundler class?

the simplest way should be by implementing parcelable and then just use

@State SimCardLists simCardLists;

then i dont need to check savedInstanceState is null or not null?
for example fetch data from database if savedInstanceState is null,
please see below my code, Thanks

@State
List<SimCardLists> simLists;

...

if (savedInstanceState == null) {
    simLists = realm.where(SimCardLists.class).findAll();
}
simcardListItems.setLayoutManager(new LinearLayoutManager(context));
adapter = new SimCardListAdapter(context, simLists, simcardListItems);
simcardListItems.setAdapter(adapter);

I get this error:

Error:(41, 24) error: Don't know how to put a java.util.List<com.pishguy.androidapplication.simcardappliaction.Models.SimCardLists> inside a Bundle

I am not sure I have understand this senario

Thanks @k0shk0sh,

I'm closing this unless there is any further question