1

How do I get out of an activity and restore it in exactly what it have in its previous state? In that activity, I have spinner. If i select 2nd item from drop-down, i get out from that activity and again go back, the activity is getting reloading. But i want to restore what exactly the activity have previously?

spinner.post(new Runnable() {
        public void run() {
            spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    dealerCd_fromDD = dealerInsp.getMap2().get(
                            dealerInsp.getLoadseqArray()[arg2]);
                    GlobalVehicle.setVehList(loadVehicles(dealerCd_fromDD));
                    loadHosts(GlobalVehicle.getVehList());
                    dealerDetails(dealerCd_fromDD, GlobalVehicle.getDealerObject());
                    loadDefaults(arg2);
                    pos=arg2;
                    deliveryInspDb.delete("damages");
                }

                @Override
                public void onNothingSelected(AdapterView<?> arg0) {

                }
            });
        }
    });

Please any one can help me on this..

I tried to call finish(), but i am coming to that activity from 3 different activities. so in that situation its not working..

user1441341
  • 495
  • 2
  • 9
  • 23
  • you only want spinner selcted data or whole data ? – Ishtiaq Sep 05 '12 at 10:29
  • i don't know what is your snario but when i worked something like this so i made a signlton class and on **new Intent** and **on back key pressed** i saved data in siglton, and when i reached again to activity i loaded it from singlton class where i saved before, you can use statics – Ishtiaq Sep 05 '12 at 10:32
  • Could you please post some example code of that..? – user1441341 Sep 05 '12 at 10:40

3 Answers3

0

http://wptrafficanalyzer.in/blog/implement-swiping-between-tabs-with-viewpager-in-action-bar-using-sherlock-library/ Try using Fragment. You can follow up the link.

Syamantak Basu
  • 905
  • 4
  • 10
  • 20
0

You can store your data to SharedPreferences on the onStop() or onPause() and retrieve it onRestart() event of your activity.

        @Override
        protected void onStop() {
            // TODO Auto-generated method stub
            super.onStop();
            SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
            SharedPreferences.Editor editor = settings.edit();                      
            editor.putInt("Selected", spinner.getSelectedItemPosition());
            editor.commit();
        }
     ....

    @Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();          
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        spinner.setSelection(settings.getInt("Selected", 0));           
    }

You can also retrieve it onCreate(), if you want that your data should be restored even if your activity was killed

Update: If you are coming from different activities, you can try Intent.FLAG_ACTIVITY_REORDER_TO_FRONT, this will bring your activity to front if it was not killed.

ThePCWizard
  • 3,338
  • 2
  • 21
  • 32
0

Try using onSaveInstanceState for saving the activity state and onRestoreInstanceState FOR RETREIVING THE ACTIVITY STATE.. Check out this link, it will probably solve the issue. https://stackoverflow.com/a/151940/1626878

Community
  • 1
  • 1
Sahil Mahajan Mj
  • 11,033
  • 8
  • 53
  • 100