[英]How to call onCreate method in 1st activity after finishing 2nd activity(which is called by the 1st activity)
I am a begginer in android. 我是android的初学者。 I have a simple code, where I am calling one activity from my 1st activity and in that 2nd activity I have a button on press of which 2nd activity is finished and 1st activity comes up.
我有一个简单的代码,在这里我从第一个活动中调用一个活动,在第二个活动中,我有一个按钮可以按下,第二个活动已完成,第一个活动出现了。 Is there any way to call the onCreate method in the 1st activity as the onCreate method is never called in the 1st activity(onResume is called always)?
有什么方法可以在第一个活动中调用onCreate方法,因为从未在第一个活动中调用过onCreate方法(总是调用onResume)? Do i have edit some thing in the manifest file.
我是否在清单文件中编辑了某些内容。
Following is my Code 以下是我的代码
public class Activity1 extends Activity {
/** Called when the activity is first created. */
TextView mTextView ;
Button b1;
static int count=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTextView = (TextView) findViewById(R.id.textView2);
if (savedInstanceState == null) {
mTextView.setText("Welcome to HelloAndroid!");
} else {
mTextView.setText("Welcome Back!");
System.out.println("count------>"+ count);
}
final Intent i = new Intent(this,activity2.class);
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(i);
}
});
}
@Override
public void onResume()
{
super.onResume();
System.out.println("inside Resume");
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putBoolean("MyBoolean", true);
savedInstanceState.putDouble("myDouble", 1.9);
savedInstanceState.putInt("MyInt", 1);
savedInstanceState.putString("MyString", "Welcome back to Android");
count++;
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
boolean myBoolean = savedInstanceState.getBoolean("MyBoolean");
double myDouble = savedInstanceState.getDouble("myDouble");
System.out.println("MyBoolean"+ myBoolean);
System.out.println("myDouble"+ myDouble);
}
}
and here is my 2nd activity which is called by the 1st activiy 这是我的第二项活动,第一项活动称为
public class activity2 extends Activity{
TextView textview;
Button b1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
textview = (TextView) findViewById(R.id.textView1);
textview.setText("in activity2");
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
Some one please help me Thanks! 有人请帮助我,谢谢!
In first Activity,call finish()
after startActivity(i);
在第一个活动中,在
startActivity(i);
之后调用finish()
startActivity(i);
and in second Activity,start first Activity before calling finish()
.When you start an Activity that has not instance,it's onCreate
will be called. 在第二个Activity中,在调用
finish()
之前启动第一个Activity。当您启动一个没有实例的Activity时,它将调用onCreate
。
Edit: 编辑:
If you want to save state of first Activity,you can create a bundle and add state of your views to it.Then add this bundle as extra to intent that starts second Activity.In second Activity get this extra from intent and when you want to start first Activity via intent(for example startFirstActivity intent) add that bundle to this intent(startFirstActivity intent).Now in onCreate
method of first Activity,get bundle from intent(via getIntent().getextras()
) and if it was not null,extract state of your views from it and set your views states after find them by ID. 如果要保存第一个Activity的状态,则可以创建一个包并向其中添加视图的状态。然后将该包作为额外的内容添加到启动第二个Activity的intent中。通过intent启动第一个Activity(例如startFirstActivity intent)将该包添加到该intent(startFirstActivity intent)中。现在在第一个Activity的
onCreate
方法中,从intent中获取包(通过getIntent().getextras()
),如果不为null ,从中提取视图状态,并在通过ID找到它们后设置视图状态。
Just use finish(); 只需使用finish(); before startActivity(i) in Activity 1
在活动1中的startActivity(i)之前
use this code in second activity2
button click, before moving to Activity2 call finish()
in Activity1
after statActivity();
在第二个
activity2
按钮单击中使用此代码,然后移到Activity2,然后在statActivity();
之后在Activity1
调用finish()
statActivity();
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Activity2.this ,Activity1.class);
startActivity(i);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.