Rahul
Rahul

Reputation: 69

Refresh MainActivity on returning from preference setting screen

I have got Preference screen . I want to instantly execute the settings changed in preference screen to MainActivity's listview

Preference.xml

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="Sys apps">
<CheckBoxPreference
    android:title="Sytem Apps"
    android:key="system"
    android:defaultValue="true"
    android:summary="Click To View Only Installed Apps"
    />
</PreferenceCategory>
</PreferenceScreen>

MainActivity.class

public class MainActivity extends ListActivity {
PackageManager packageManager;
List<ApplicationInfo> applist;
Listadapter listadapter;
String packageName="";
boolean isUnInstallClicked;
int mPosition;
boolean installed;
ApplicationInfo info;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    packageManager = getPackageManager();

    new LoadApplications().execute();

}   

@Override
protected void onResume() {
    super.onResume();

    if(isUnInstallClicked && !appInstalledOrNot(packageName)){
        applist.remove(listadapter.getItem(mPosition));
        listadapter.notifyDataSetChanged();
    }

}


private boolean appInstalledOrNot(String uri){
    PackageManager pm=getPackageManager();
    boolean app_installed;
    try{
        pm.getPackageInfo(uri,PackageManager.GET_ACTIVITIES);
        app_installed=true;
    }catch (PackageManager.NameNotFoundException e){
        app_installed=false;
    }
    return app_installed;
}

private List<ApplicationInfo> checkForLaunchIntent(List<ApplicationInfo> list) {
    ArrayList<ApplicationInfo> applist = new ArrayList<>();
    SharedPreferences preferences= PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    installed=preferences.getBoolean("system", true);
    for (ApplicationInfo info : list) {
        try {
            if(installed==true) {
                if ((info.flags & ApplicationInfo.FLAG_SYSTEM)!=0) {
                    applist.add(info);
                    listadapter.notifyDataSetChanged();
                }
            }
            else if(installed==false){
                if ((info.flags & ApplicationInfo.FLAG_SYSTEM)==0) {
                    applist.add(info);
                    listadapter.notifyDataSetChanged();
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    return applist;
}

private class LoadApplications extends AsyncTask<Void, Void, Void> {
    private ProgressDialog progress = null;

    @Override
    protected Void doInBackground(Void... params) {
        applist = checkForLaunchIntent(packageManager.getInstalledApplications(PackageManager.GET_META_DATA));

        listadapter = new Listadapter(MainActivity.this, R.layout.list_item, applist);

        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        setListAdapter(listadapter);
        progress.dismiss();
        super.onPostExecute(aVoid);

    }

    @Override
    protected void onPreExecute() {
        progress = ProgressDialog.show(MainActivity.this, null, "loading apps info,,,");
        super.onPreExecute();
    }

}

@Override
protected void onPause() {
    super.onPause();

}
}

I want that when "system==false" , mainactivity's listview gets updated with installed apps as soon as i click back and return on mainactivity.

Upvotes: 1

Views: 81

Answers (1)

Karakuri
Karakuri

Reputation: 38585

Load the applications list in onStart() instead of onCreate(). (There are some other options if this is not to you liking, but this one is probably the easiest.)

Upvotes: 1

Related Questions