Use PreferenceFragment in Bottom Navigation Tabbed Application - android

Main Activity:
switch (item.getItemId()) {
case R.id.action_item1:
selectedFragment = MeetingsFragment.newInstance();
break;
case R.id.action_item2:
selectedFragment = FriendsFragment.newInstance();
break;
case R.id.action_item3:
selectedFragment = PreferenceFragment.newInstance();
break;
}
//selectedFragment.setArguments(bundle);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout, selectedFragment);
transaction.commit();
All the fragments extend android.support.v4.app.Fragment except PreferenceFragment(extends android.preference.PreferenceFragment).
Compiler is looking for android.support.v4.app.Fragment. I want to show preferences in one of the tab. Is their any solution to this?

Let your PreferenceFragment extend PreferenceFragmentCompat.

use PreferenceFragmentCompat which is included in the support-v7 library
this class inherit from Fragment in v4 so it should work for you

Related

Switching between PreferenceFragment and android.support.v4.app.Fragment;

I have a BottomNavigationView where I can navigate between fragments. The third fragment is a PreferenceFragment while the other two extend Fragment. I have a simple switch case for replacing fragments but I get an error that SettingsFragment cannot be converted to Fragment.
How can I navigate between these fragments?
BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.navigation_home:
toolbar.setTitle("Home");
selectedFragment = HomeFragment.newInstance();
break;
case R.id.navigation_dashboard:
toolbar.setTitle("Dashboard");
selectedFragment = DashboardFragment.newInstance();
break;
case R.id.navigation_settings:
toolbar.setTitle("Settings");
selectedFragment = SettingsFragment.newInstance();
break;
}
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragmentContainer, selectedFragment);
transaction.commit();
return true;
}
};
Unless you're using the support preference library, PreferenceFragment extends android.app.Fragment, so you need to also extend android.app.Fragment for your other Fragments.
However, since Fragment and PreferenceFragment have been deprecated in API 28, you might want to just convert to the support Fragment and PreferenceFragmentCompat. Be aware that this will mean any Preference libraries you might be using will probably not work until they've been updated.

Correct Architecture for Bottom Navigation Bar Android Application

I would like to get some input on the best way to structure my app's architecture when using Android's Bottom Navigation View.
Currently I define my BottomNavigationView in my MainActivity. It looks something like this.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView bottomNavigationView = (BottomNavigationView)findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()){
case R.id.action_home:
selectedFragment = HomeFragment.newInstance();
break;
case R.id.action_search:
selectedFragment = SearchFragment.newInstance();
break;
case R.id.action_message:
selectedFragment = MessageFragment.newInstance();
break;
case R.id.action_profile:
selectedFragment = ProfileFragment.newInstance();
break;
}
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout, selectedFragment);
transaction.commit();
return true;
}
});
//Manually displaying the first fragment - one time only
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout, HomeFragment.newInstance());
transaction.commit();
}
The problem is that once I click on one tab, it opens up a fragment, and I would like to have those Fragments open up other Fragments/Activities (i.e:
I open the profile tab (`ProfileFragment` loads)
I click on a button from `ProfileFragment`, and from this the `SignUpFragment` loads
After running into many bugs, I've researched on how to architect my app, but i've found mixed results. Would anyone know the correct way of using a BottomNavigationView with Fragments, and in those fragments I can load more Activities/fragments. A huge thanks in advance.

Switch activity/fragment with bottom navigation

I created an activity with a bottom navigation bar.
I googled a lot things about it but now I don't know how to handle this exactly.
Before, I just started another activity when the user clicks the bottom navigation but I think it's not good.
How can I switch between the tabs?
Do I have to work with fragments? And what about 'setContentView(int layoutResID)'? How can I do that? I'm confused...
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
return true;
case R.id.navigation_dashboard:
startActivity(dashboardActivity);
return true;
case R.id.navigation_notifications:
startActivity(dashboardActivity);
return true;
}
return false;
}
};
Thank you very much for your help - I hope, you understood what I mean.
Activity transition is always expensive and we should switch from one activity to another only when we are switching the context. A fragment is a portion of UI in an activity. Same fragment can be used with multiple activities. Just like activity a fragment has its own lifecycle and setContentView(int layoutResID) can be set to different layout in OnCreate of fragment.
This link explains more on when to use activity or fragment.
Android developer guide on Fragments
Code path tutorial on bottom navigation views.
Please refer to :-
https://github.com/waleedsarwar86/BottomNavigationDemo
and complete explanation in
http://waleedsarwar.com/posts/2016-05-21-three-tabs-bottom-navigation/
You will get a running code with the explanation here.
Bottom Navigation View is a navigation bar introduced in android library to make it easy to switch between views with a single tap. It can although be used for almost any purpose, but is most commonly used to switch between fragments with a single tap. Its use for opening activities is somewhat absurd, since it ignores its most important functionality of switching the views with a single tap. There are many good articles and blogs out there in this regard, one of which is:
https://medium.com/#hitherejoe/exploring-the-android-design-support-library-bottom-navigation-drawer-548de699e8e0
Hope this solves your doubt..
bottomNavigationView.setOnNavigationItemSelectedListener
(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.action_item1:
selectedFragment = ItemOneFragment.newInstance();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout, selectedFragment);
transaction.commit();
// selectedFragment.getChildFragmentManager().beginTransaction();
break;
case R.id.action_item2:
selectedFragment = ItemTwoFragment.newInstance();
FragmentTransaction transactiona = getSupportFragmentManager().beginTransaction();
transactiona.replace(R.id.frame_layout, selectedFragment);
transactiona.commit();
// selectedFragment = ItemThreeFragment.newInstance();
break;
case R.id.action_item3:
// selectedFragment = ItemOneFragment.newInstance();
Intent intent=new Intent(MainView.this, YoutActivityLive.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
// selectedFragment = ItemTwoFragment.newInstance();
break;
case R.id.action_item5:
selectedFragment = ItemOneFragment.newInstance();
FragmentTransaction transactionb = getSupportFragmentManager().beginTransaction();
transactionb.replace(R.id.frame_layout, selectedFragment);
transactionb.commit();
// selectedFragment = ItemFiveFragment.newInstance();
break;
}
return true;
}
});

Replacing a fragment

I've implemented the bottom navigation view in an activity which I use to navigate between fragments. Apparently, there is some strange behavior when switching between fragments. Whenever I log out of my app and log in with the a different account. Details of the former account are shown occasionally. This happens when I switch to the same fragment continuously. The database I'm using is realm, and all the data is usually cleared out on logout. I have verified. Whenever I logout, the activity with the fragments is destroyed using finish() So how is it that I'm getting fragments with data from the previous account. Are fragments left in the stack event after the parent activity is removed? The issue is really bothering because I can't really explain the cause. The code for replacing fragments is as follows:
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
boolean initialSync = Preferences.getBoolean(MainActivity.this, "initialSync");
if (!initialSync && item.getItemId() != R.id.navigation_logout) {
InitialSyncFragment fragment = new InitialSyncFragment();
transaction.replace(R.id.content, fragment);
transaction.commit();
return true;
}
getFirstName();
switch (item.getItemId()) {
case R.id.navigation_home:
transaction.replace(R.id.content, new HomeFragment());
break;
case R.id.navigation_grades:
transaction.replace(R.id.content, new GradesFragment());
break;
case R.id.navigation_finances:
transaction.replace(R.id.content, new FinancesFragment());
break;
case R.id.navigation_info:
transaction.replace(R.id.content, new InfoFragment());
break;
case R.id.navigation_logout:
logout();
break;
}
transaction.commit();
return true;

Android app: How to change fragments in Navigation Drawer

First of all I'm new to android developement and also to java.
I created a new project with the fragment navigation drawer,
but I don't really know how to add and change to those new fragments with it.
(It also seems like it doesnt change any fragments, just sets the TextView to a the selected position.)
Do you guys may know a good tutorial on how to do that (step by step)?
Or can you please give me a hint on how to do it?
EDIT:
I found a solution by myself:
#Override
public void onNavigationDrawerItemSelected(int position) {
Fragment fragment;
switch(position){
case 0:
fragment = new Fragment_main().newInstance(position+1);
break;
case 1:
fragment = new Fragment_two().newInstance(position+1);
break;
default:
fragment = new Fragment_main().newInstance(position +1);
break;
}
// update the main content by replacing fragments
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit();
}
you just have to add a switch into this method, and also create those classes (and layouts).
I hope this solution helps everyone who also struggles as begginer or with the navigation drawer :)
I found a solution by myself:
#Override
public void onNavigationDrawerItemSelected(int position) {
Fragment fragment;
switch(position){
case 0:
fragment = new Fragment_main().newInstance(position+1);
break;
case 1:
fragment = new Fragment_two().newInstance(position+1);
break;
default:
fragment = new Fragment_main().newInstance(position +1);
break;
}
// update the main content by replacing fragments
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit();
}
you just have to add a switch into this method, and also create those classes (and layouts).
I hope this solution helps everyone who also struggles as begginer or with the navigation drawer :)

Categories

Resources