2012-11-06 22 views
0

我有一個包含名稱列表的ListView的主要活動。點擊列表項目,第二項活動將被解僱。在第二項活動上按下返回按鈕時,我應該回到第一項活動中,與第二項活動發生時的狀態相同。我可以知道我怎麼能做到這一點..?如何存儲活動的狀態並檢索回來onResume

請附上一些示例代碼。

這裏是我的示例代碼

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); 

      } 
     }); 


    } 


} 
+0

當您從第二個活動回到它回來添加此在第一個Activity的onResume()中。 –

+2

http://stackoverflow.com/questions/151777/saving-activity-state-in-android/151940#151940 –

回答

3

有很多方法可以做到這一點。一個簡單的解決方案是

使您的索引靜態變量;

public static int index = 0; 

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

index = position; 

以下行onPuase

//index = list.getFirstVisiblePosition(); 

爲此在的onResume()

list.setSelection(index); 

//list.setSelectionFromTop(index, 0);