Hide activity in android

I have two actions in an Android application, each has a button that jumps to another action (method startActivity()). In the second step, I have an edit text (URL address bar) and a web view.

So, when I click activity1 button to start activity2, I get activity2 with a web view. Then I type cnn.com(for example) in the address bar, and my webview displays the website cnn.com. After that, I click the button to go from activity2 to activity1. If I click on activity1 button again, I started activity2. But activity2 has just been created, I mean that the edit text and web view are empty.

I want: if I return from activity1 to activity2, I want Activity2 to keep the last state: the edit text should contain cnn.comand the webview should show the CNN website. Therefore, I need not to leave Activity2 before starting activity1, but something like just hiding and starting activity2, so if I return to it again, I will get its last state. (Like, for example, when I press the home button.) How can I achieve this?

+4
source share
3 answers

Here is a suggestion to solve your problem.

when you switch from the button that is in the second action, and now you are in the first exercise, make sure that when switching from the second action to one, it should not complete the second action.

, , , .

    Intent mIntent=new Intent(yourActivityOne1.this, YourActivity2.class);
    mIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    mIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    startActivity(mIntent);

, , Activity , Activity, , .

+9

, , , , 2 onPause , 2.

2 :

onPause(){
    SharedPreferences settings = getSharedPreferences("MyPreferences", MODE_PRIVATE);  
    SharedPreferences.Editor prefEditor = settings.edit();  
    prefEditor.putString("activity2Text", textField.getText().toString());  
    prefEditor.commit(); 
}

onCreate(){
      .....
    SharedPreferences settings = getSharedPreferences("MyPreferences", MODE_PRIVATE); 
    textField.setText(settings.getString("activity2Text","") );
}
+1

BackPressed,

.. finsih

@Override
public void onBackPressed()
    {

   Log.d("CDA", "onBackPressed Called");
   Intent setIntent = new Intent(Intent.ACTION_MAIN);
   setIntent.addCategory(Intent.CATEGORY_HOME);
   setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   startActivity(setIntent);
   }
+1

All Articles