我有3个班级A,B,C。
A类将3个值传递给B类,B类将3个值传递给C类。 在C级我有一个后退按钮如果我点击这个后退按钮我想恢复我的活动B.
我是Android新手;我对活动生命周期知之甚少。提前谢谢。
这是我的代码。
public class A extends Activity
{
EditText one,two,three;Button sub;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.one);
this.onWindowFocusChanged(true);
one=(EditText)findviewById(R.id.one);
two=(EditText)findviewById(R.id.one);
three=(EditText)findviewById(R.id.one);
addListenerOnButton();
}
public void addListenerOnButton()
{
// TODO Auto-generated method stub
sub.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Intent i = new Intent(A.this, B.class);
Bundle b=new Bundle();
b.putString("one",one.getText().toString());
b.putString("two",two.getText().toString());
b.putString("three",three.getText().toString());
b.putExtras(b);
startActivity(i);
}
});
}
}
这是B类活动
public class B extends Activity
{
EditText one,two,three;Button sub;String one1,two1,three1;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.two);
this.onWindowFocusChanged(true);
one=(EditText)findviewById(R.id.one);
two=(EditText)findviewById(R.id.one);
three=(EditText)findviewById(R.id.one);
Bundle b = this.getIntent().getExtras();
one1=b.getString("one");
one.setText(one1);
two1=b.getString("two");
two.setText(two1);
three1=b.getString("three");
three.setText(three1);
addListenerOnButton();
}
public void addListenerOnButton()
{
// TODO Auto-generated method stub
sub.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Intent i = new Intent(B.this, C.class);
Bundle b=new Bundle();
b.putString("one",one.getText().toString());
b.putString("two",two.getText().toString());
b.putString("three",three.getText().toString());
b.putExtras(b);
startActivity(i);
}
});
}
}
这是C类活动(应用程序崩溃)
public class C extends Activity
{
EditText one,two,three;Button sub,back;String one1,two1,three1;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.three);
this.onWindowFocusChanged(true);
one=(EditText)findviewById(R.id.one);
two=(EditText)findviewById(R.id.one);
three=(EditText)findviewById(R.id.one);
Bundle b = this.getIntent().getExtras();
one1=b.getString("one");
one.setText(one1);
two1=b.getString("two");
two.setText(two1);
three1=b.getString("three");
three.setText(three1);
addListenerOnButton();
}
public void addListenerOnButton()
{
back.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
//Clicking back button i want to resume the activity b class but its getting crashed
Intent i = new Intent(C.this, B.class);
startActivity(i);
}
});
}
}
每当我点击此后退按钮时,应用程序崩溃,而不是恢复我的活动B.
答案 0 :(得分:2)
要关闭C活动并恢复最后一个活动,请使用finish();
例如,将finish();
放入后退按钮侦听器
答案 1 :(得分:0)
在Activity C类中用这个替换你的后退按钮监听器:
back.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
finish();
}
}
它将关闭您当前的活动(C),从而恢复您之前处于活动状态的活动(B)。
答案 2 :(得分:0)
像这样使用
public class Main1Activity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
((TextView)findViewById(R.id.txt)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
onBackPressed();
}
});
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
}
}