0

I have MainActivity with mapFragment inside of it. And I have settings button that starts new activity with settings.The following code starts it

@Override
public void onClick(View v) {
    if(v == findViewById(R.id.btnSettings)){
        Intent AlarmPreferencesActivityIntent;
        AlarmPreferencesActivityIntent = new Intent(App.getContext(), AlarmPreferencesActivity.class);
        AlarmPreferencesActivityIntent.putExtra("alarm", alarm);
        App.getContext().startActivity(AlarmPreferencesActivityIntent);
    }
}

Manifest declaration of activity

<activity
        android:name=".Preferences.AlarmPreferencesActivity"
        android:label="@string/title_activity_alarm_preference"
        android:parentActivityName=".MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.geoalarm.MainActivity" />
    </activity>

That settings activity has auto-generated back button in menu(you can see on screenshot). enter image description here

So problem is when I press that button it doesn't return me back to MainActivity, but creates it again. Method onDestroy of MainActivity triggers when I press that back button in settings activity. Can I do something to get MainActivity by clicking back button that was created before I started activity with settings?

Blind Despair
  • 3,190
  • 2
  • 20
  • 31

2 Answers2

1

I just recollected I had a similar issue in one of my applications,

I used this code in my settings activity which extends PreferenceActivity and implements Preference.OnPreferenceChangeListener

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public Intent getParentActivityIntent() {
        return super.getParentActivityIntent().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    }

Please add this to your manifest

<activity android:name=".SettingsActivity"
            android:label="@string/title_activity_setting"
            android:parentActivityName=".MainActivity"
            android:theme="@style/SettingsTheme">>
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.package.MainActivity" />
        </activity>

on options selected simply i created an intent of the settings activity and used it.

This worked in my case. Hope this helps you.

0

Try removing this line of code

AlarmPreferencesActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Maybe this will help.

Randheer
  • 984
  • 6
  • 24