我有一个包含名称列表的 ListView 的主要活动。单击列表项时,将触发第二个活动。在第二个活动上按下 BACK 按钮时,我应该在触发第二个活动时以相同的状态返回到第一个活动。我可以知道我怎么能做到这一点..?
请附上一些示例代码。
这是我的示例代码
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);
}
});
}
}