Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hide activity in android

I have two activities in an android application, each has a button that goes to the other activity (by startActivity() method). In the second activity, I have an edit text (URL address bar) and a web view.

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

What I want is: if I return from activity1 to activity2, I want activity2 to keep the last state: the edit text should keep cnn.com and the web view should keep displaying the CNN web site. So what I need is not to quit activity2 before starting activity1, but something like just hiding it and starting activity2, so that if I return to it again, I get its last state. (Like when I click on the home button.) How can I achieve this?

like image 551
Alaoui Ghita Avatar asked May 03 '12 16:05

Alaoui Ghita


People also ask

How do I make Android activity only appear once?

Now in the onResume() method, check if we already have the value of prevStarted in the SharedPreferences. Since it will evaluate to false when the app is launched for the first time, start the MainActivity (Which we want to appear just once) and set the value of prevStarted in the SharedPreferences.

How can I hide app bar in Android?

If you want to hide Action Bar from the entire application (from all Activities and fragments), then you can use this method. Just go to res -> values -> styles. xml and change the base application to “Theme. AppCompat.


3 Answers

Here is a suggestion to solve your problem.

when you go from Button that is in Activity two and now you are in Activity one make sure that when you move from activity two to one, it should not finish activity two.

Now when you want to go back to Activity two So you need to call your Activity like this in below.

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

so what this code does is it will make Activity to come over first without creating a new instance of activity and you can see that your last loaded website it there.

like image 159
Herry Avatar answered Sep 29 '22 09:09

Herry


It will not finish the activity on BackPressed it will behave as Home Button

You can edit it .. it may help not to 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);
   }
like image 33
Zar E Ahmer Avatar answered Sep 29 '22 09:09

Zar E Ahmer


I don't know how to hide it but what I will do is save text from activity 2 into preferences in onPause method and load data from preferences when I am creating activity 2.

Activity 2 will looks like:

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","") );
}
like image 31
Martin Vandzura Avatar answered Sep 29 '22 08:09

Martin Vandzura