[英]How can I resume Activity B from Activity C?
I have 3 classes A,B,C. 我有3个班级A,B,C。
Class A am passing 3 values to class B and class B am passing 3 values to class C. In class CI have one back button if I click this back button I want to resume my activity B. A类将3个值传递给B类,B类将3个值传递给C类。在CI类中有一个后退按钮如果单击此后退按钮我想恢复我的活动B.
I am new for android; 我是android新手; I don't know much about activity life cycle.
我对活动生命周期知之甚少。 Thanks in advance.
提前致谢。
This is my code. 这是我的代码。
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);
}
});
}
}
This is the class B Activity 这是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);
}
});
}
}
This is class C Activity ( where the app crashes ) 这是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);
}
});
}
}
Whenever I click this back button the app crashes, not resuming my Activity B. 每当我点击此后退按钮时,应用程序崩溃,而不是恢复我的活动B.
To close the C activity and resume the last one, use finish();
要关闭C活动并恢复最后一个活动,请使用
finish();
So for example, put finish();
例如,put
finish();
in your back button listener 在你的后退按钮监听器中
Replace your back button listener with this in your Activity C class: 在Activity C类中用这个替换你的后退按钮监听器:
back.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
finish();
}
}
It will close your current Activity (C), and therefore resume your Activity that was active before it (B). 它将关闭您当前的活动(C),从而恢复您之前处于活动状态的活动(B)。
Use like this 像这样使用
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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.