suresh cheemalamudi
suresh cheemalamudi

Reputation: 6240

How to store activity's state and retrieve back onResume

I have a main activity with a ListView containing a list of names. On click of the list item, 2nd activity will be fired. On pressing BACK button on the 2nd activity, I should come back to the 1st activity with the same state as that, at the time of firing second activity. may I know how could I achieve this..?

Please attach some sample code.

here is my sample code

Countries_List_Activity.java




public class Countries_list_Activity extends Activity 
{
     String[] countries_list = new String[] {
             "India",
             "Pakistan",
             "Sri Lanka",
             "China",
             "Bangladesh",
             "Nepal",
             "Afghanistan",
             "North Korea",
             "South Korea",
             "Japan",
             "Australia",
             "GreenLand",
             "Las vegas",
             "U.K",
             "Canada",
             "Zimbabwe",
             "Netherland",
             "Singapore",
             "Dubai",
             "Burma",
             "Sutherland",
             "Weat Indies",
             "New Zealand",
             "Kenya",
             "Namibia"
     };

     int index = 0;
     ListView list;
     Context context=this;


    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

         list = (ListView) findViewById(R.id.listview);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, countries_list);

        list.setAdapter(adapter);


        list.setOnItemClickListener(new OnItemClickListener() 
        {
            public void onItemClick(AdapterView<?> parent, View view,int position, long id) 
            {
                Object tmp = parent.getItemAtPosition(position);
                String name=tmp.toString();
                System.out.println(name);

                Intent myIntent = new Intent();
                //myIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                myIntent.setClassName("com.mink7.countries","com.mink7.countries.Country_Selected_Activity");
                myIntent.putExtra("name", name);
                startActivity(myIntent);
            }
        });


    }


    protected void onPause()
    {
        super.onPause();

        index = list.getFirstVisiblePosition();
        // Save scroll position
       /* SharedPreferences preferences = context.getSharedPreferences("SCROLL", 0);
        SharedPreferences.Editor editor = preferences.edit();
        int scroll = list.getScrollY();
        editor.putInt("ScrollValue", scroll);
        editor.commit();*/
    }

    @Override
    protected void onResume()
    {
        super.onResume();

        list.setSelectionFromTop(index, 0);
        // Get the scroll position
        /*SharedPreferences preferences = context.getSharedPreferences("SCROLL", 0);
        int scroll = preferences.getInt("ScrollView", 0);
        list.scrollTo(0, scroll);*/
    }

    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}


Country_selected_Activity.java



package com.mink7.countries;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Country_Selected_Activity extends Activity
{
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.selected_country_display);
        TextView tv=(TextView) findViewById(R.id.textView1);
        Button b=(Button) findViewById(R.id.button1);

        Bundle extras = getIntent().getExtras();

        String name =extras.getString("name");
        tv.setText(name);

        b.setOnClickListener(new OnClickListener() 
        {
            public void onClick(View v)
            {
                Intent intent=new Intent();
                intent.setClassName("com.mink7.countries","com.mink7.countries.Countries_list_Activity");
                startActivity(intent);

            }
        });


    }


}

Upvotes: 0

Views: 678

Answers (1)

kumar
kumar

Reputation: 691

There are many ways to do that. one simple solution is

Make your index static variable;

  public static int index = 0;

add this in

onItemClick(AdapterView<?> parent, View view,int position, long id)

 index = position;

the below line in onPuase

//index = list.getFirstVisiblePosition();

do this in onResume()

 list.setSelection(index);

//list.setSelectionFromTop(index, 0);

Upvotes: 3

Related Questions