-1

When I press the Logout button in the Navigation drawer the app logouts but comes to homepage and freezes. How can I stop that?

HomeActivity

public class HomeActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {


    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.drawer_layout)
    DrawerLayout mDrawer;
    @BindView(R.id.nav_view)
    NavigationView navigationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        ButterKnife.bind(this);

        mToolbar.setNavigationIcon(R.drawable.nav_drwaer_option);
        changeToolbarTitle("Home");
        setSupportActionBar(mToolbar);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, mDrawer, mToolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);

        toggle.setDrawerIndicatorEnabled(false);

        toggle.setToolbarNavigationClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mDrawer.isDrawerOpen(GravityCompat.START)) {
                            mDrawer.closeDrawer(GravityCompat.START);
                        } else {
                            mDrawer.openDrawer(GravityCompat.START);
                        }
                    }
                });

        mDrawer.addDrawerListener(toggle);
        toggle.syncState();


        navigationView.setNavigationItemSelectedListener(this);
        navigationView.setItemIconTintList(null);

        DashBoardFragment dashBoardFragment = new DashBoardFragment();
        showFragment(dashBoardFragment);
    }

    public void changeToolbarTitle(String title) {
        mToolbar.setTitle(title);
    }
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        int id = item.getItemId();
        if(id == R.id.add_asset){

        }
        else if(id == R.id.location_update){
            gotoSpecificFragment(new AssetListFragment(),"Assets");
        }
        else if(id == R.id.work_order)
        {

        }
        else if(id == R.id.by_date)
        {

        }
        else if(id == R.id.by_name) {

        }
        else if(id == R.id.by_work)
        {

        }
        else if(id == R.id.security_setting)
        {

        }
//problem is here i think
        else if (id == R.id.logOutBusiness) {
            SaveUserInformationUtil.clearUserInfo(this);
            SaveUserInformationUtil.clearUserProfile(this);
//            Intent intent = new Intent(HomeActivity.this,HomePage_Register_Login.class);
//            startActivity(intent);
            finish();
        }



//        else if(id == R.id.nav_login){
//            Intent loginIntent = new Intent(HomeActivity.this,NewLoginActivity.class);
//            startActivity(loginIntent);
//        }
//        else if(id == R.id.nav_register){
//            Intent registrationIntent = new Intent(HomeActivity.this, NewRegisterActivity.class);
//            startActivity(registrationIntent);
//        }
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if(drawer.isDrawerOpen(GravityCompat.START))
            drawer.closeDrawer(GravityCompat.START);
        else
            drawer.openDrawer(GravityCompat.START);
        return true;
    }

    private void showFragment(Fragment fragment) {
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.dashboard_container, fragment, "Home")
                .commit();
    }

    private void gotoSpecificFragment(Fragment fragment, String title) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        fragmentTransaction.replace(R.id.dashboard_container, fragment);
        fragmentTransaction.addToBackStack(String.valueOf(mToolbar.getTitle()));


        fragmentTransaction.commit();
        changeToolbarTitle(title);
    }

}

Before login out happens I am saving the data in SharedPreference with pref.apply() for background writing and deleting, and after logout happens the app returns to the homepage and its get freezes. LogCat does not show any error. For further reference posting the logcat information below.

10-09 15:11:31.655 1861-1935/system_process D/ActivityManager: TopActivityInfo, pkgName: com.yumatechnology.yumatelematics activityName: com.yumatechnology.yumatelematics/.activity.HomePage_Register_Login callingPackage:   bstSpecialAppKeyboardHandlingEnabled = false
10-09 15:11:31.655 1861-1935/system_process D/ActivityManager: Sending app_launch intent for appName: yumatelematics pkgName: com.yumatechnology.yumatelematics
10-09 15:11:31.655 1861-1935/system_process D/WindowManager: in computeScreenConfigurationLocked() -- hardKeyboardAvailable :true  mHardKeyboardAvailable :true   mHardKeyboardEnabled :true
10-09 15:11:31.675 1861-3690/system_process D/dalvikvm: GC_FOR_ALLOC freed 2194K, 63% free 9395K/24832K, paused 11ms, total 12ms
10-09 15:11:31.695 1861-3690/system_process D/dalvikvm: GC_FOR_ALLOC freed 2266K, 63% free 9352K/24832K, paused 11ms, total 11ms
10-09 15:11:31.695 1861-3579/system_process V/WindowManager: isVisibleLw false for win : Window{4eb07fb4 u0 SurfaceView}
10-09 15:11:31.705 9715-9744/? E/PGA: eglGetConfigAttrib: Not Implemented: attribute EGL_ALPHA_MASK_SIZE
10-09 15:11:31.705 9715-9744/? E/PGA: eglGetConfigAttrib: Not Implemented: attribute EGL_ALPHA_MASK_SIZE
10-09 15:11:31.705 9715-9744/? E/PGA: eglGetConfigAttrib: Not Implemented: attribute EGL_ALPHA_MASK_SIZE
10-09 15:11:31.705 9715-9744/? E/PGA: eglGetConfigAttrib: Not Implemented: attribute EGL_ALPHA_MASK_SIZE
10-09 15:11:31.705 9715-9744/? E/PGA: eglGetConfigAttrib: Not Implemented: attribute EGL_ALPHA_MASK_SIZE
10-09 15:11:31.705 9715-9744/? E/PGA: eglGetConfigAttrib: Not Implemented: attribute EGL_ALPHA_MASK_SIZE
10-09 15:11:31.705 9715-9744/? E/PGA: eglGetConfigAttrib: Not Implemented: attribute EGL_ALPHA_MASK_SIZE
10-09 15:11:31.705 9715-9744/? E/PGA: eglGetConfigAttrib: Not Implemented: attribute EGL_ALPHA_MASK_SIZE
10-09 15:11:31.705 9715-9744/? E/PGA: eglGetConfigAttrib: Not Implemented: attribute EGL_ALPHA_MASK_SIZE
10-09 15:11:31.705 9715-9744/? E/PGA: eglGetConfigAttrib: Not Implemented: attribute EGL_ALPHA_MASK_SIZE
10-09 15:11:31.705 9715-9744/? E/PGA: eglGetConfigAttrib: Not Implemented: attribute EGL_ALPHA_MASK_SIZE
10-09 15:11:31.705 9715-9744/? E/PGA: eglGetConfigAttrib: Not Implemented: attribute EGL_ALPHA_MASK_SIZE
10-09 15:11:31.705 9715-9744/? E/PGA: eglGetConfigAttrib: Not Implemented: attribute EGL_ALPHA_MASK_SIZE
10-09 15:11:31.705 9715-9744/? E/PGA: eglGetConfigAttrib: Not Implemented: attribute EGL_ALPHA_MASK_SIZE
10-09 15:11:31.705 9715-9744/? E/PGA: eglGetConfigAttrib: Not Implemented: attribute EGL_ALPHA_MASK_SIZE
10-09 15:11:31.705 9715-9744/? E/PGA: eglGetConfigAttrib: Not Implemented: attribute EGL_ALPHA_MASK_SIZE
10-09 15:11:31.725 9715-9744/? W/mbgl: {Thread-484}[OpenGL]: Not using Vertex Array Objects
10-09 15:11:32.455 1861-2288/system_process V/WindowManager: isVisibleLw false for win : Window{4eb07fb4 u0 SurfaceView}
10-09 15:11:32.475 1861-3689/system_process V/WindowManager: isVisibleLw false for win : Window{4eb07fb4 u0 SurfaceView}
10-09 15:11:32.475 1861-2050/system_process D/InputMethodManagerService: packageName=com.yumatechnology.yumatelematics, activityName=.activity.HomePage_Register_Login
10-09 15:11:32.475 1861-2050/system_process D/InputMethodManagerService: ime_enabled = false is same as last value, no change
10-09 15:11:32.475 1861-3663/system_process V/WindowManager: isVisibleLw false for win : Window{4eb07fb4 u0 SurfaceView}
10-09 15:11:32.485 1861-1874/system_process V/WindowManager: isVisibleLw false for win : Window{4eb07fb4 u0 SurfaceView}
10-09 15:11:32.485 1861-1874/system_process V/WindowManager: isVisibleLw false for win : Window{4eb327f0 u0 com.yumatechnology.yumatelematics/com.yumatechnology.yumatelematics.activity.HomeActivity}
10-09 15:11:32.485 1861-1874/system_process V/WindowManager: isVisibleLw false for win : Window{4ee863d0 u0 SurfaceView}
10-09 15:11:32.515 1861-3687/system_process D/dalvikvm: GC_FOR_ALLOC freed 2208K, 63% free 9362K/24832K, paused 18ms, total 18ms
10-09 15:11:32.525 1861-3687/system_process V/WindowManager: isVisibleLw false for win : Window{4eb327f0 u0 com.yumatechnology.yumatelematics/com.yumatechnology.yumatelematics.activity.HomeActivity}
10-09 15:11:32.825 9391-9439/? D/dalvikvm: GC_FOR_ALLOC freed 1794K, 25% free 6239K/8264K, paused 7ms, total 7ms
10-09 15:11:32.885 1514-1514/? W/SurfaceFlinger: couldn't log to binary event log: overflow.

Also including the xml file of the HomeActivity

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_launcher"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/textColorWhite"
        android:fitsSystemWindows="true"
        android:minHeight="?android:listPreferredItemHeightSmall"
        app:headerLayout="@layout/nav_header"
        app:itemTextColor="@color/textColorBlack"
        app:menu="@menu/menu_drawer"
        app:theme="@style/NavigationViewTextAppearance" />

</android.support.v4.widget.DrawerLayout>

Also attaching the menu_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/add_asset"
        android:title="Asset Management"
        android:icon="@drawable/add_assets"
        >

    </item>
    <item
        android:id="@+id/work_order"
        android:title="Work Order"
        android:icon="@drawable/list"
        />
    <item
        android:id="@+id/location_update"
        android:title="Location Update"
        android:icon="@drawable/list"

        />

    <item android:title="Locate Asset"
        >
        <menu>
            <item
                android:id="@+id/by_date"
                android:title="Locate By Date "
                android:icon="@drawable/bangladesh"
                >

            </item>
            <item
                android:id="@+id/by_work"
                android:title="Locate WorkOrder"
                android:icon="@drawable/citymap"
                >

            </item>

        <item
            android:id="@+id/by_name"
            android:title="Locate Date+Asset"
            android:icon="@drawable/citymap"
            >

        </item>
    </menu>
</item>


<item android:title="">


    <item
        android:id="@+id/security_setting"
        android:title="Setting"
        android:icon="@drawable/shield"
        >

    </item>
    <item
        android:id="@+id/logOutBusiness"
        android:title="Log Out"
        android:icon="@drawable/shield"
        >

    </item>

</item>

1 Answers1

0

You can try to adapt the code from this answer: https://stackoverflow.com/a/5140545/3780625

Try this in your menu:

 Intent intent = new Intent(getApplicationContext(), HomePage_Register_Login.class);
 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
 intent.putExtra("LOGOUT", true);
 startActivity(intent);

And then in your Login activity, before doing anything else on the onCreate, you clear the data.

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getIntent().getBooleanExtra("LOGOUT", false)) {
            SaveUserInformationUtil.clearUserInfo(this);
            SaveUserInformationUtil.clearUserProfile(this);
        }

        setContentView(R.layout.activity_home);
}

Hope it helps !

Maxouille
  • 2,729
  • 2
  • 19
  • 42
  • I tried with the way you mentioned here. Its staying the same. Here in the pastebin i am giving the log and code that i have coded after getting your answer. (Log) (https://pastebin.com/GawKPzEK) (code) (https://pastebin.com/AzcAyXe2) – Mohibul Hassan Oct 09 '18 at 17:09
  • Do you really get the intent in the mainActivity ? Can you add a `Log.d()` and see if it's called ? Can you also post you `SaveUserInformationUtil` class ? – Maxouille Oct 10 '18 at 11:51