Search code examples
androidextension-methodsrestartandroid-context

getBaseContext() undefined how to solve without extension


This code should restart my app:

Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
getContext().startActivity(i);

but I get a massege getBaseContext() is undefined for the type ButtonView.PhoneCallListener how can I sove that without changing the extention's?

I tried getContext() instead but that got me to the main screen and not to the app.


Solution

  • What you obviously need is the application's context. This can be obtained via the Context.getApplicationContext() method. And you can obtain a Context instance via View.getContext().

    To put it in a nutshell, you can have access to application's context from the view code with:

    getContext().getApplicationContext();
    

    So this may work:

    Context appContext = getContext().getApplicationContext();
    Intent i = appContext .getPackageManager().getLaunchIntentForPackage(appContext .getPackageName());
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    getContext().startActivity(i);