Question

I Have a very annoying managing activities problem.

I have 2 activities :

1- Main acitivty

2- Preference Activity launched by the main activity.

On PreferenceActivity when i tap "previous" button the application finish instead of returning to the main activity ! I debuged the code and i noticed that the main activity is always destroyed just after onCreate() method of my preference activity is called !

There is how i call preference activity :

// Launch Settings activity
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);

Also my preference activity class :

public class SettingsActivity extends PreferenceActivity {

    private static ListView listView;

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getFragmentManager().beginTransaction()
                .replace(android.R.id.content, new MyPreferenceFragment())
                .commit();

        // Setting the list's background to be white
        listView = (ListView) findViewById(android.R.id.list);
    }

    @SuppressLint("NewApi")
    public static class MyPreferenceFragment extends PreferenceFragment
            implements OnPreferenceClickListener,
            ColorPickerDialog.OnColorChangedListener {

        /**
         * Background color preference.
         */
        private Preference prefBgColor;

        /**
         * Text color preference.
         */
        private Preference prefTextColor;

        @Override
        public void onCreate(final Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.settings);

            final Preference prefBgColor = findPreference("bg_color");
            prefBgColor
                    .setOnPreferenceClickListener(new OnPreferenceClickListener() {

                        public boolean onPreferenceClick(Preference arg0) {

                            // Create color picker dialog
                            new ColorPickerDialog(getActivity(),
                                    MyPreferenceFragment.this, prefBgColor
                                            .getKey(), prefBgColor
                                            .getSharedPreferences().getInt(
                                                    prefBgColor.getKey(), 0),
                                    prefBgColor.getSharedPreferences().getInt(
                                            prefBgColor.getKey(), 0)).show();

                            return true;
                        }
                    });

            final Preference prefTextColor = findPreference("text_color");
            prefTextColor
                    .setOnPreferenceClickListener(new OnPreferenceClickListener() {

                        public boolean onPreferenceClick(Preference arg0) {

                            // Create color picker dialog
                            new ColorPickerDialog(getActivity(),
                                    MyPreferenceFragment.this, prefTextColor
                                            .getKey(), prefTextColor
                                            .getSharedPreferences().getInt(
                                                    prefTextColor.getKey(), 0),
                                    prefTextColor.getSharedPreferences()
                                            .getInt(prefTextColor.getKey(), 0))
                                    .show();

                            return true;
                        }
                    });

        }

        @SuppressLint("NewApi")
        @Override
        public boolean onPreferenceClick(Preference pref) {

            return true;
        }

        @SuppressLint("NewApi")
        @Override
        public void colorChanged(String key, int color) {

            // Get choosed color and save it
            this.findPreference(key).getEditor().putInt(key, color).commit();
        }
    }

}

Edit : There is the activities declaration in my manifest file :

     <activity
            android:name="com.meher.tools.menulauncher.MainActivity"
            android:label="@string/app_name"
            android:configChanges="orientation|screenSize"
            android:theme="@style/AppTheme"
            android:launchMode="standard" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    <!-- Settings activity  -->
    <activity 
        android:name="com.meher.tools.menulauncher.SettingsActivity"
        android:configChanges="orientation|screenSize"
        android:theme="@style/AppTheme"
        android:launchMode="standard" >
    </activity>

I will really appreciate any help.

Was it helpful?

Solution

I resolved this problem, this happens because i overrided onPause() in MainActivity with finish(), for this reason my main activity is destroyed when i open another activity...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top