I have class that extends Activity. That has a “Logout” menu options. When I click that it calling logout(context) from another class (not extends from Activity).
public class MyTask extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listlayout);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mytaskmenu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.logout:
Functions.logout(getBaseContext());
return true;
default:
return true;
}
}
}
public class Functions {
public static void logout(Context context)
{
DbAdapter_User db2= new DbAdapter_User(context);
db2.open();
db2.handleLogout();
db2.close();
context.startActivity(new Intent(context, LogIn.class));
}
And the error is
android.util.AndroidRuntimeException:
Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
WHAT IS GOING WRONG?
You need to call the context of the MyTask activity in the functions class, by passing the context of the activity, instead of passing
Functions.logout(getBaseContext());tryFunctions.logout(this.getContext());