1

I have a problem with the lifecycle of the activities on android. I am trying to return back from my second activity to my first one. It actually my code is doing that, not the way it supposed to do it. I have "back button" on the top left corner and when I hit it - it goes through onDestroy() and onCreate() methods of my MainActivity. But if I use the back button on my phone - everything is OK(it goes just trough onResume() method) Can someone explain me - where my code is wrong?

in MainActivity.java:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.settings_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
        startActivity(intent);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

in my SettingsActivity.java:

package com.example.garagedoor;

import android.os.Bundle;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.PreferenceFragmentCompat;

public class SettingsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings_activity);
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.settings, new SettingsFragment())
                .commit();
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    public static class SettingsFragment extends PreferenceFragmentCompat {
        @Override
        public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
            setPreferencesFromResource(R.xml.root_preferences, rootKey);
        }
    }
}

in AndroidManifest.xml:

<activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".SettingsActivity"
        android:label="@string/title_activity_settings"
        android:parentActivityName=".MainActivity">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity"/>
    </activity>
Veselin
  • 67
  • 5

2 Answers2

1

You need to add

android:launchMode="singleTop"

to the manifest entry for MainActivity. See the following questions:

ActionBar up navigation recreates parent activity instead of onResume

How can I return to a parent activity correctly?

David Wasser
  • 93,459
  • 16
  • 209
  • 274
0

Your problem is probably about the task stack. I am not sure what actionbar button do but i think you should either use

@Override public void onBackPressed()

in SettingsActivity or start the SettingsActivity with Intent flags like here:

https://developer.android.com/guide/components/activities/tasks-and-back-stack

Hope this helps.

Numan Karaaslan
  • 1,365
  • 1
  • 17
  • 25