Opening an Activity and returning saving the main state

Asked

Viewed 598 times

1

After many researches I came across the following situation: I’m on my own Main Activity and would like to open another Activity(passing parameters to Activity2[In case a meditate]), after this use the back button on Toolbar and return to Main.

inserir a descrição da imagem aqui

Problems: I’m unable to maintain the class instance main, when the return is made by Actionbar is made a new creation of Mainactivity, thus losing the current state of the main.

Note1: Parameter passages are ok, only missing the main instance.

 intent2 = new Intent(this, meditate.class);

public void passartextos(String tituloE, String textoE){
        intent2.putExtra("titulo", tituloE);
        intent2.putExtra("texto", textoE);
        startActivity(intent2);

    }

In Activity meditate was made the changes in XML according to a topic of the forum BR Stackoverflow Problems with getSupportActionBar() to Home (arrow) button and back arrow

In the class meditate has been introduced supportactionbar line

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

and in the Androidmanifest

<activity android:name=".meditate"
            android:parentActivityName=".MainActivity"></activity>

When you’re a kid, it comes down to:

Mainactivity -> Meditate

Meditate -> New Mainactivity

  • What do you mean? When you return you are setting another onCreate?

  • What do you want to happen when you click the back button? Should you press something in the previous class? I don’t get it very well.

  • When I return is done another Oncreate and when I return I just want to give Finish na Activity Meditate.

  • On your device’s native back, when you click while on Meditate what happens?

  • Go back to Main the way I want, without running onCreate.

1 answer

1


Insert the onOptionsItemSelected in your class Meditate using android.R.id.home to finalize the Activity current. See:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // ação voltar do action bar home/up 
    switch (item.getItemId()) {
        case android.R.id.home:
            onBackPressed();
            return true;
    }
    return super.onOptionsItemSelected(item);
}

So your code should look like this:

public class Meditate extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_meditate);

        assert getSupportActionBar() != null;
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // ação voltar do action bar home/up 
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
  • Ack I’m having problems implementing this method, I’m using appcompatactivity. As shown below: http://imgur.com/a/aBLm4

  • @Sayoanoliveira your method has to be out of onCreate

  • Even outside onCreate it does not recognize the Menuitem class. http://imgur.com/a/ybZb4

  • @Sayoanoliveira post your entire code, otherwise it’s hard to know what the problem is.

  • At hand: http://pastebin.com/fLArYfFq

  • @Sayoanoliveira you have to import the Menuitem. See : import android.view.Menuitem;

  • Ack all right! I just wondered why the AS did not give import suggestion. THANK YOU SO MUCH! I stayed 4 hours to solve this problem.

  • @Sayoanoliveira happens! Good luck there!!

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.