名前のリストを含む ListView を使用したメイン アクティビティがあります。リスト項目をクリックすると、2 番目のアクティビティが起動されます。2番目のアクティビティでBACKボタンを押すと、2番目のアクティビティの発火時に、それと同じ状態で1番目のアクティビティに戻るはずです。どうすればこれを達成できるか分かりますか..?
サンプルコードを添付してください。
これが私のサンプルコードです
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);
}
});
}
}