Android perform button click event on navigation drawer item selected - java

I am using an Android material navigation drawer for my app and am trying to create a log out button as one of the navigation drawer items
#Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
// Toast.makeText(this, "Menu item selected -> " + position, Toast.LENGTH_SHORT).show();
Fragment fragment = null;
switch (position) {
case 0:
fragment = new AnotherFragment();
break;
case 1:
fragment = new AnotherFragment();
break;
case 2:
fragment = new AnotherFragment();
break;
case 3:
fragment = new AnotherFragment();
break;
case 4:
fragment = new AnotherFragment();
break;
case 5:
fragment = new AnotherFragment();
break;
case 6:
fragment = new AnotherFragment;
break;
case 7:
fragment = new AnotherFragment();
break;
case 8:
fragment = new AnotherFragment();
break;
case 9:
break;
default:
break;
}
if (fragment != null)
{
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment).commit();
}
on case 9 I want to allow the user to log out,
Intent logOut = new Intent(GroupHomeActivity.this, LoginActivity.class);
getSharedPreferences("Pref", Context.MODE_PRIVATE).edit().clear().commit();
logOut.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
How can I allow the codes above to be executed on case 9?

Well here is what I think you should do :
Fragment fragment = null;
switch (position) {
case 0:
fragment = new AnotherFragment();
break;
case 1:
fragment = new AnotherFragment();
break;
case 2:
fragment = new AnotherFragment();
break;
case 3:
fragment = new AnotherFragment();
break;
case 4:
fragment = new AnotherFragment();
break;
case 5:
fragment = new AnotherFragment();
break;
case 6:
fragment = new AnotherFragment;
break;
case 7:
fragment = new AnotherFragment();
break;
case 8:
fragment = new AnotherFragment();
break;
case 9:
fragment=null;
break;
default:
break;
}
if (fragment != null)
{
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment).commit();
}
else {
Intent logOut = new Intent(GroupHomeActivity.this, ElgarLoginActivity.class);
getSharedPreferences("Pref", Context.MODE_PRIVATE).edit().clear().commit();
logOut.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
}
Simply because the return type is expecting a non null fragment. Well just return a null fragment, and on null logout the user.

Here is how I would handle the logic for the position 9 click
#Override
public void onNavigationDrawerItemSelected(int position) {
if(position == 9){
Intent logOut = new Intent(GroupHomeActivity.this, ElgarLoginActivity.class);
getSharedPreferences("Pref", Context.MODE_PRIVATE).edit().clear().commit();
logOut.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
}else{
switchFragment(position);
}
}
Here is how you would switch the fragments.
private void switchFragment(int position){
Fragment fragment = null;
switch (position) {
case 0:
fragment = new AnotherFragment();
break;
case 1:
fragment = new AnotherFragment();
break;
case 2:
fragment = new AnotherFragment();
break;
case 3:
fragment = new AnotherFragment();
break;
case 4:
fragment = new AnotherFragment();
break;
case 5:
fragment = new AnotherFragment();
break;
case 6:
fragment = new AnotherFragment;
break;
case 7:
fragment = new AnotherFragment();
break;
case 8:
fragment = new AnotherFragment();
break;
default:
break;
}
if (fragment != null){
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment).commit();
}
}

Just put your code after case 9 before break. Like this:
Case 9:
Intent logOut = new Intent(GroupHomeActivity.this, ElgarLoginActivity.class);
getSharedPreferences("Pref", Context.MODE_PRIVATE).edit().clear().commit(); logOut.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
StartActivity(logout);
break;

Related

fragment bottom navigation bar clickable one time selected item

When I click again to any item, same fragment view reloading every time. But I wanna do bottom navigation bar item one time clickable. how can i do this?
This is MainActivity.java codes
navigationMenu=findViewById(R.id.navigation);
navigationMenu.setOnNavigationItemSelectedListener(navListener);
//navigationMenu.getMenu().getItem(2).setIcon(R.drawable.arrow_down);
//I added this if statement to keep the selected fragment when rotating the device
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new UserHomeFragment()).commit();
}
This is click listener codes
private BottomNavigationView.OnNavigationItemSelectedListener navListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.navigation_home:
selectedFragment = new UserHomeFragment();
break;
case R.id.navigation_works:
selectedFragment = new UserWorksFragment();
break;
case R.id.navigation_completed_works:
selectedFragment = new UserCompletedFragment();
break;
case R.id.navigation_profile:
selectedFragment = new UserAccountFragment();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
selectedFragment).commit();
return true;
}
};

Fragment Acitivty recreates fragments after the app is paused

When I pause the app for a few moments the activity's Fragmnetos are recreated
public class CopycatActivity extends FragmentActivity implements CopycatInterface {
public void flipFragment(int type) {
FragmentTransaction ft = getSupportFragmentManager()
.beginTransaction();
switch (type) {
case 1:
ft.replace(R.id.container, zWaitFragment);
break;
case 2:
ft.replace(R.id.container, zMasterMode);
break;
case 3:
ft.replace(R.id.container, zCopyMode);
break;
case 4:
ft.replace(R.id.container, zRateDraws);
break;
case 5:
ft.replace(R.id.container, zWinnerFragment);
break;
case 6:
ft.replace(R.id.container, zSleepMode);
break;
}
ft.commitAllowingStateLoss();
}
}
Depending on which fragment I am in, when paused it is reset

How to properly initialise fragment to null?

So here's the code:
private void selectItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new DoctorFragment();
break;
case 2:
fragment = new HospitalFragment();
break;
case 3:
fragment = new SpecialClinicFragment();
break;
case 4:
fragment = new PharmacyFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
//the parameter "fragment" in this line of code gives an error message.
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
getActionBar().setTitle(mNavigationDrawerItemTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
Log.e("MainActivity", "Error in creating fragment");
}
It seems like I haven't properly initialised fragment instance.
The error message was "Wrong 2nd argument type" but I'm pretty sure I put in the right parameter.
Problem is not with the argument.You have used Fragment from
android.support.v4 library but you are making transaction on android.app.fragment.
Solution:
import android.app.fragment instead of android.support.v4.fragment
Because you cannot mix up with support.v4 library and android.app.*

tablayout fragment's content not updating

I have an activity with a TabLayout. When I go to my 2nd activity where I add some datas which are used in 1st activity and I come back, I need like 5-10 sec and a lot of swipes fot allowing the 1st activity fragment to be updated.
This is how I use the
#Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
tabLayout = (TabLayout) findViewById(R.id.tab_layout);
viewPager = (ViewPager) findViewById(R.id.viewPager);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
}
setTitle("La mia settimana:");
calendar = Calendar.getInstance();
dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
choosenDate = calendar.getTime();
viewPager.setAdapter(new SectionPagerAdapter(getSupportFragmentManager()));
tabLayout.setupWithViewPager(viewPager);
viewPager.setCurrentItem((dayOfWeek == 1 ? 7 : dayOfWeek - 1));
}
My adapter is the following:
public class SectionPagerAdapter extends FragmentPagerAdapter {
public SectionPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
choosenDate = GetDateFromDayWeek(2);
break;
case 1:
choosenDate = GetDateFromDayWeek(3);
break;
case 2:
choosenDate = GetDateFromDayWeek(4);
break;
case 3:
choosenDate = GetDateFromDayWeek(5);
break;
case 4:
choosenDate = GetDateFromDayWeek(6);
break;
case 5:
choosenDate = GetDateFromDayWeek(7);
break;
case 6:
choosenDate = GetDateFromDayWeek(8);
break;
default:
break;
}
HomePageTabFragment homePageTabFragment = new HomePageTabFragment();
Bundle args = new Bundle();
args.putString("date", globalDateFormat.format(choosenDate));
args.putInt("day", dayOfWeek);
homePageTabFragment.setArguments(args);
return homePageTabFragment;
}
#Override
public int getCount() {
return 7;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Lun";
case 1:
return "Mar";
case 2:
return "Mer";
case 3:
return "Gio";
case 4:
return "Ven";
case 5:
return "Sab";
case 6:
return "Dom";
default:
return "Giorni settimana";
}
}
}
I always call the same fragment, with a Bundle of arguments.
I'm not gonna post the fragment, because it's really long and not relevant.
I just explain what he does: He take from db all entities, he sum some data's columns and he shows those datas inside the list.
now, my problem is the following:
In the 2nd activity I add an item to a list, and I save it in local db.
When i come back to my 1st activity, the datas (which are relative to ALL entities in db) should be updated.
The datas need like 20 sec of gestures before getting updated.
How can I solve this?

How i can use .replace(Fragment , mapsFragment)? Android

I have use .replace() for replace a old Fragment with a new, but in this moment i do no how use the same of .replace() by a Fragment to a mapsFragment.
Ideas?!
I can use replace? if i can' t use it, what I can use?
This is my code in this moment. (the .replace() don' t work)
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getFragmentManager();
mapsFragment fragM = null;
Fragment frag = null;
switch (position) {
case 0: //Home
mTitle = "Maps";
fragM = new mapsFragment();
break;
case 1: //Query utente
mTitle = "Section 2";
break;
case 2: //Query Amministratore
mTitle = "Section 3";
break;
}
if (position != 3) {
if(position==0){
fragmentManager.beginTransaction()
.replace(R.id.container, fragM)
.commit();
}else {
fragmentManager.beginTransaction()
.replace(R.id.container, frag)
.commit();
}
I have find the solution:link where i have found it
This is the code in particular:
if(position==0){
FragmentTransaction mTransaction = getSupportFragmentManager()
.beginTransaction();
SupportMapFragment mFRaFragment = new mapsFragment();
mTransaction.replace(R.id.container, mFRaFragment);
mTransaction.commit();
}else {

Categories