I have this navigation drawer built with placeholder view, however when I press logout button it doesn't start the activity I'd like to start after logout. Here is the code, could someone explain to me what I did wrong?
Thanks
@Layout(R.layout.drawer_item)
public class DrawerMenuItem {
public static final int DRAWER_MENU_ITEM_LOGOUT = 8;
private int mMenuPosition;
private Context mContext;
private DrawerCallBack mCallBack;
public DrawerMenuItem(Context context, int menuPosition) {
mContext = context;
mMenuPosition = menuPosition;
}
@Resolve
private void onResolved() {
switch (mMenuPosition){
case DRAWER_MENU_ITEM_LOGOUT:
itemIcon.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_exit_to_app_black_24dp));
itemNameTxt.setText("Logout");
break;
}
}
@Click(R.id.mainView)
private void onMenuItemClick(){
switch (mMenuPosition){
case DRAWER_MENU_ITEM_LOGOUT:
Toast.makeText(mContext, "Logout", Toast.LENGTH_SHORT).show();
if(mCallBack != null)mCallBack.onLogoutMenuSelected();
mContext.startActivity(new Intent (mContext,MainActivity.class));
break;
}
}
public void setDrawerCallBack(DrawerCallBack callBack) {
mCallBack = callBack;
}
public interface DrawerCallBack{
void onLogoutMenuSelected();
}
}
Here's my Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.albej.orederveg">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<!-- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> -->
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AddFood"
android:windowSoftInputMode="adjustResize"/>
<activity android:name=".OpenOrders" />
<activity android:name=".LoginActivity" />
<activity android:name=".FarmerMenuActivity" />
<activity android:name=".DetailsActivity" />
<activity android:name=".ItemListActivity" />
<activity android:name=".SingleFoodActivity" />
<activity android:name=".SingleOrderActivity" />
<activity android:name=".MainTabActivity"></activity>
</application>
</manifest>
And here my main activity that I'd like to start on logout
public class MainActivity extends AppCompatActivity {
private EditText email,pass,loc,name;
private DatabaseReference mDatabase;
private FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
email=(EditText) findViewById(R.id.editEmail);
pass=(EditText) findViewById(R.id.editPass);
mAuth= FirebaseAuth.getInstance();
mDatabase= FirebaseDatabase.getInstance().getReference().child("Farmers");
}
//Signing up
public void signupButtonClicked(View view){
final String email_text=email.getText().toString().trim();
String pass_text=pass.getText().toString().trim();
if(!TextUtils.isEmpty(email_text) && !TextUtils.isEmpty(pass_text)){
mAuth.createUserWithEmailAndPassword(email_text,pass_text).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
String user_id = mAuth.getCurrentUser().getUid();
DatabaseReference current_user = mDatabase.child(user_id);
current_user.child("email").setValue(email_text)
Intent detailsIntent = new Intent(MainActivity.this, DetailsActivity.class);
startActivity(detailsIntent);
}
}
});
}
}
public void signinButtonClicked(View view){
Intent loginIntent=new Intent(MainActivity.this,LoginActivity.class);
startActivity(loginIntent);
}
}
Below the Logcat
01-27 18:15:37.953 10376-10376/com.example.albej.orederveg W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
01-27 18:15:52.743 10376-10574/com.example.albej.orederveg W/System.err: remove failed: ENOENT (No such file or directory) : /data/user/0/com.example.albej.orederveg/cache/picasso-cache/journal.tmp
01-27 18:16:37.193 10376-10376/com.example.albej.orederveg D/ViewRootImpl: ViewPostImeInputStage processPointer 0
01-27 18:16:37.293 10376-10376/com.example.albej.orederveg D/ViewRootImpl: ViewPostImeInputStage processPointer 1
01-27 18:16:37.313 10376-10376/com.example.albej.orederveg W/System.err: java.lang.reflect.InvocationTargetException
01-27 18:16:37.313 10376-10376/com.example.albej.orederveg W/System.err: at java.lang.reflect.Method.invoke(Native Method)
01-27 18:16:37.313 10376-10376/com.example.albej.orederveg W/System.err: at com.mindorks.placeholderview.ViewBinder$1.onClick(ViewBinder.java:159)
01-27 18:16:37.313 10376-10376/com.example.albej.orederveg W/System.err: at android.view.View.performClick(View.java:5697)
01-27 18:16:37.313 10376-10376/com.example.albej.orederveg W/System.err: at android.view.View$PerformClick.run(View.java:22526)
01-27 18:16:37.313 10376-10376/com.example.albej.orederveg W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
01-27 18:16:37.313 10376-10376/com.example.albej.orederveg W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
01-27 18:16:37.313 10376-10376/com.example.albej.orederveg W/System.err: at android.os.Looper.loop(Looper.java:158)
01-27 18:16:37.313 10376-10376/com.example.albej.orederveg W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7224)
01-27 18:16:37.313 10376-10376/com.example.albej.orederveg W/System.err: at java.lang.reflect.Method.invoke(Native Method)
01-27 18:16:37.313 10376-10376/com.example.albej.orederveg W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
01-27 18:16:37.313 10376-10376/com.example.albej.orederveg W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
01-27 18:16:37.313 10376-10376/com.example.albej.orederveg W/System.err: Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
01-27 18:16:37.313 10376-10376/com.example.albej.orederveg W/System.err: at android.app.ContextImpl.startActivity(ContextImpl.java:734)
01-27 18:16:37.313 10376-10376/com.example.albej.orederveg W/System.err: at android.app.ContextImpl.startActivity(ContextImpl.java:721)
01-27 18:16:37.313 10376-10376/com.example.albej.orederveg W/System.err: at android.content.ContextWrapper.startActivity(ContextWrapper.java:345)
01-27 18:16:37.313 10376-10376/com.example.albej.orederveg W/System.err: at com.example.albej.orederveg.DrawerMenuItem.onMenuItemClick(DrawerMenuItem.java:120)
01-27 18:16:37.313 10376-10376/com.example.albej.orederveg W/System.err: ... 11 more
01-27 18:16:37.323 10376-10376/com.example.albej.orederveg D/SecWifiDisplayUtil: Metadata value : none
01-27 18:16:37.323 10376-10376/com.example.albej.orederveg D/ViewRootImpl: #1 mView = android.widget.LinearLayout{fd99f00 V.E...... ......I. 0,0-0,0 #10203a7 android:id/toast_layout_root}
01-27 18:16:37.373 10376-10376/com.example.albej.orederveg D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1
01-27 18:16:37.393 10376-10376/com.example.albej.orederveg W/DisplayListCanvas: DisplayListCanvas is started on unbinded RenderNode (without mOwningView)
01-27 18:16:39.333 10376-10376/com.example.albej.orederveg D/ViewRootImpl: #3 mView = null
01-27 18:17:39.763 10376-10634/com.example.albej.orederveg V/FA: Recording user engagement, ms: 107315
01-27 18:17:39.773 10376-10634/com.example.albej.orederveg V/FA: Using measurement service
01-27 18:17:39.773 10376-10634/com.example.albej.orederveg V/FA: Connecting to remote service
01-27 18:17:39.783 10376-10634/com.example.albej.orederveg V/FA: Activity paused, time: 181999657
01-27 18:17:39.793 10376-10634/com.example.albej.orederveg D/FA: Logging event (FE): user_engagement(_e), Bundle[{firebase_event_origin(_o)=auto, engagement_time_msec(_et)=107315, firebase_screen_class(_sc)=MainTabActivity, firebase_screen_id(_si)=6299809065178216245}]
01-27 18:17:39.803 10376-10634/com.example.albej.orederveg V/FA: Using measurement service
01-27 18:17:39.803 10376-10634/com.example.albej.orederveg V/FA: Connection attempt already in progress
01-27 18:17:39.863 10376-10376/com.example.albej.orederveg V/ActivityThread: updateVisibility : ActivityRecord{3b9ab2c token=android.os.BinderProxy@fc50a4f {com.example.albej.orederveg/com.example.albej.orederveg.MainTabActivity}} show : true
01-27 18:17:39.863 10376-10634/com.example.albej.orederveg D/FA: Connected to remote service
01-27 18:17:39.863 10376-10634/com.example.albej.orederveg V/FA: Processing queued up service tasks: 2
01-27 18:17:44.913 10376-10634/com.example.albej.orederveg V/FA: Inactivity, disconnecting from the service
01-27 18:20:51.153 10376-10428/com.example.albej.orederveg W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
Replacing
MainTabActivity maintab=new MainTabActivity();
maintab.Logout();
with
SaveSharedPreference.clearUserName(mContext);
mContext.startActivity(new Intent (mContext, MainActivity.class));
should solve the issue. If you prefere to have this part of the code in sepparate method, you should keep that method in DrawerMenuItem
.
If it's still not working please check is there anything in Logcat when you click on the button and also add code from AndroidManifest.xml
and MainActivity.java
to your question.
Update:
Problem is pretty clear from this line from LogCat log:
Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
This means that the mContext
is not Activity's context.
To fix this make sure that when creating DrawerMenuItem
you use Activity's context. In the line where you have something like this: new DrawerMenuItem(context, menuPosition)
, context
needs to be Activity.