1

I am making a simple app to download video from a website using volley.

So when i opened my app for first time everything was fine but when i opened app second time after closing app with back button , then i got a error or warning. Now i can't use alertDialogs ( process dialogs are working fine) because of it ( app crashed if i did)

i am using singleton class AlertUser for AlertDialogs

public class AlertUser {
    private static final AlertUser AlertUserInstance  = new AlertUser();
    private Context ActivityContext = null;

    private AlertUser() { }

    public static AlertUser getInstance() {
        return AlertUserInstance;
    }

    public void init(Context ctx) {
        if (ActivityContext == null)
            ActivityContext = ctx;
    }


    public void alertUser(String msg) {
        if (!((AppCompatActivity)ActivityContext).isFinishing()) {
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ActivityContext);
            alertDialogBuilder.setTitle(R.string.app_name);
            alertDialogBuilder.setMessage(msg);
            alertDialogBuilder.setPositiveButton("Got it!",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {
                            
                        }
                    });
            AlertDialog alertDialog = alertDialogBuilder.create();
            alertDialog.show();
        }
    }
}

Here is some code from MainActivity

public class MainActivity extends AppCompatActivity {

    private RequestQueue queue;

    private AlertUser alert = AlertUser.getInstance();

    ProgressDialog progressDialog;

    @Override
    public void onCreate(Bundle saveInstanceState) {
        super.onCreate(saveInstanceState);
        setContentView(R.layout.activity_main);
        Log.d("Main","onCreate");
        alert.init(MainActivity.this);

        queue = new Volley().newRequestQueue(MainActivity.this);

    }
}

Edit : Now i am using System.exit(0) and now it's working in every run after being killed but i don't think its good solution and still i am unaware from exact problem.

Golu
  • 350
  • 2
  • 14

0 Answers0