I am a begginer in 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)? 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);
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.
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.
Just use finish(); before startActivity(i) in Activity 1
use this code in second activity2
button click, before moving to Activity2 call finish()
in Activity1
after statActivity();
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Activity2.this ,Activity1.class);
startActivity(i);
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.