Basically,I have two activity
1. FirstActivity.java
and
2. SecondActivity.java
.
In this SecondActivity.java
I have one Button that works as swapping between this two activity.So, when I press that Button at that time SecondActivity.java
activity swapped with FirstActivity.java
and when I close my application and restart again my application display that swapped operation means SecondActivity.java
activity swapped successfully with the FirstActivity.java
.
So,Guys If anyone know how can I do this then give me some idea to Implement this kind of functionality.
You need to properly terminate your application.
@Override
public void onBackPressed() {
AlertDialog.Builder al=new AlertDialog.Builder(this);
al.setTitle(R.string.app_name);
al.setMessage(R.string.wanna_exit);
al.setIcon(R.drawable.back);
al.setCancelable(false);
al.setPositiveButton(R.string.yea,new DialogInterface.OnClickListener(){public void onClick(DialogInterface id, int s){
moveTaskToBack(true);
getIntent().removeExtra("isInit"); // remove if you're using any extra
android.os.Process.killProcess(android.os.Process.myPid());
finish();
System.exit(1);
}});
al.setNegativeButton(R.string.nope,new DialogInterface.OnClickListener(){public void onClick(DialogInterface id, int s){id.cancel();}});
al.create().show();
}
Use finish()
when you want to move from one activity to another.
@Override
public void onBackPressed() {
Intent intent=new Intent(getApplicationContext(),FirstActivity.class);
startActivity(intent);
finish();
}
In your SecondActivity add following lines in onCreate() :
SharedPreference pref = getSharedPreferences("yourPref", MODE_PRIVATE);
String activityName = pref.getString("activityName",null);
if(activityName != null && activityName.equals("FirstActivity")){
startActivity(new Intent(this,FirstActivity.class));
finish();
}
In clicklistner of button of SecondActivity.java class add following lines:
SharedPreference pref = getSharedPreferences("yourPref", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString("activityName", "FirstActivity");
editor.commit();
In clicklistener of button of FirstActivity.java class add following lines :
SharedPreference pref = getSharedPreferences("yourPref", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString("activityName", "SecondActivity");
editor.commit();
Lets make some order here. "switching" activities is a very confusing expression to say. I would suggest you that when the user click the button at one of those activities you will save a number in Intent
.
For example:
0 - open the first activity.
1 - open the second activity.
I'm guessing your app is launching from the first activity.
When the application first run, retrieve this number from the Inent
. If its a 0
do nothing. If its a 1
- use: startActivity(new Intent(FirstActivity.this, SecondActivity.class))
to start the 2nd activity and right after, use finish()
to close FirstActivity
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.