App crashes when I press the Up(back) button in settings menu

1

so I'm still fairly new to Android Programming, and have been working on an app. I just created a settings menu in which all the settings are held, and I'm having an issue that when the back button in the action bar is pressed, the app crashes. If I press the back button on the phone itself, everything is fine, but it only crashes when the back button on the settings action bar is pressed.

I've looked through my MainActivity.java file and I noticed this stops happening when I remove my onDestroy() function, however with the way my app works, I need to have this code in my onDestory function in order for my app to close completely whenever I close it. Here is my onDestory Function:

//Kill app completely when app is closed
@Override
protected void onDestroy() {

    super.onDestroy();

        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        startActivity(intent);
        int pid = android.os.Process.myPid();
        android.os.Process.killProcess(pid);

}

I have tried changing the contents to say finish() and various other fixes, but nothing works. It almost seems as if nothing in the main page is saving.

My SettingsActivity.java file:

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);
        }
    }
}

Here is my Logcat, which doesn't really make much sense for what is happening.

2021-01-07 15:22:46.118 1881-1894/? E/memtrack: Couldn't load memtrack module
2021-01-07 15:22:46.190 1881-1966/? E/InputDispatcher: channel 'eefb3fd com.example.obd/com.example.obd.SettingsActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
2021-01-07 15:22:46.190 1881-1966/? E/InputDispatcher: channel '466b0d7 com.example.obd/com.example.obd.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
2021-01-07 15:22:46.221 2553-4044/? E/s.nexuslaunche: No package ID ff found for ID 0xffffffff.
2021-01-07 15:22:46.221 2553-4044/? E/IconLoader: Could not find icon drawable from resource
    android.content.res.Resources$NotFoundException: Resource ID #0xffffffff
        at android.content.res.ResourcesImpl.getValueForDensity(ResourcesImpl.java:225)
        at android.content.res.Resources.getDrawableForDensity(Resources.java:887)
        at android.content.res.Resources.getDrawable(Resources.java:827)
        at com.android.systemui.shared.recents.model.IconLoader.createNewIconForTask(SourceFile:118)
        at com.android.systemui.shared.recents.model.IconLoader.getAndInvalidateIfModified(SourceFile:94)
        at com.android.systemui.shared.recents.model.RecentsTaskLoader.getAndUpdateActivityIcon(SourceFile:325)
        at com.android.systemui.shared.recents.model.RecentsTaskLoadPlan.executePlan(SourceFile:188)
        at com.android.systemui.shared.recents.model.RecentsTaskLoader.loadTasks(SourceFile:173)
        at com.android.quickstep.RecentsModel.onTaskStackChangedBackground(SourceFile:214)
        at com.android.systemui.shared.system.TaskStackChangeListeners.onTaskStackChanged(SourceFile:80)
        at android.app.ITaskStackListener$Stub.onTransact(ITaskStackListener.java:50)
        at android.os.Binder.execTransact(Binder.java:731)
2021-01-07 15:22:46.309 2300-2384/? E/ModuleIdSetter: exception when setting module id
    java.lang.IllegalStateException: Unable to get current module info in ModuleManager created with non-module Context
        at com.google.android.chimera.config.ModuleManager.getCurrentModule(:com.google.android.gms@204516031@20.45.16 (100700-344294571):2)
        at afxj.a(:com.google.android.gms@204516031@20.45.16 (100700-344294571):4)
        at afxm.Y(:com.google.android.gms@204516031@20.45.16 (100700-344294571):1)
        at aftx.a(Unknown Source:0)
        at scv.a(:com.google.android.gms@204516031@20.45.16 (100700-344294571):0)
        at ryz.f(:com.google.android.gms@204516031@20.45.16 (100700-344294571):1)
        at ryx.e(:com.google.android.gms@204516031@20.45.16 (100700-344294571):1)
        at sbr.p(:com.google.android.gms@204516031@20.45.16 (100700-344294571):2)
        at sbr.v(:com.google.android.gms@204516031@20.45.16 (100700-344294571):3)
        at sbr.e(:com.google.android.gms@204516031@20.45.16 (100700-344294571):2)
        at sbv.handleMessage(:com.google.android.gms@204516031@20.45.16 (100700-344294571):71)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at afms.iD(:com.google.android.gms@204516031@20.45.16 (100700-344294571):0)
        at afms.dispatchMessage(:com.google.android.gms@204516031@20.45.16 (100700-344294571):11)
        at android.os.Looper.loop(Looper.java:193)
        at android.os.HandlerThread.run(HandlerThread.java:65)

Thanks so much for the help!

android
android-studio
asked on Stack Overflow Jan 7, 2021 by N4P • edited Jan 15, 2021 by Leo

1 Answer

0

remove:

ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
    actionBar.setDisplayHomeAsUpEnabled(true);
}

Use the below code outside your oncreate method.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
        super.onBackPressed();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

and add the below line to your activity in manifest.

        android:parentActivityName=".activity_1"

Hope this will work.

answered on Stack Overflow Jan 8, 2021 by Unknown_2433

User contributions licensed under CC BY-SA 3.0