Opening an activity and returning it saving the status of main

2

After many searches I came across the following situation: I'm on my Main Activity and would like to open another Activity (passing parameters to Activity2 ), then use the button back on the toolbar and return to the main.

Problems:Iamunabletomaintainthemainclassinstance,whentheActionBarreturnismade,MainActivity,thuslosingthecurrentstatusofmain.

Note1:Parameterpassagesareok,onlymissingthemaininstance.

intent2=newIntent(this,meditate.class);publicvoidpassartextos(StringtituloE,StringtextoE){intent2.putExtra("titulo", tituloE);
        intent2.putExtra("texto", textoE);
        startActivity(intent2);

    }

In the activity meditate the changes in the XML were done according to a forum topic BR StackOverFlow // getSupportActionBar () for button (arrow) Home and back arrow ←

In class meditate was introduced the line that supportactionbar

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

and AndroidManifest

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

In kids, the problem comes down to:

MainActivity - > Meditate

Meditate - > New MainActivity

    
asked by anonymous 21.01.2017 / 13:05

1 answer

2

Enter onOptionsItemSelected in your class Meditate using android.R.id.home to finish 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);
}

Then 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);
    }
}
    
21.01.2017 / 13:20