I want to select the start layout in my project.
For example after: After user successfully sign-in I want to see redirect him to fragment_home page but now he is redirected to friends layout. Please help.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
bottomNav.setOnNavigationItemSelectedListener(navListener);
//it might be working but it isn't
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new HomeFragment()).commit();
}
private BottomNavigationView.OnNavigationItemSelectedListener navListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.nav_home:
selectedFragment = new HomeFragment();
break;
case R.id.nav_ranking:
selectedFragment = new RankingFragment();
break;
case R.id.nav_friends:
selectedFragment = new FriendsFragment();
break;
case R.id.nav_game:
selectedFragment = new GameFragment();
break;
case R.id.nav_user:
selectedFragment = new UserFragment();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
selectedFragment).commit();
return true;
}
};
}
https://i.stack.imgur.com/RoS4o.png <<< photo
Here is my menu of BottomBar
<item
android:id="#+id/nav_game"
android:icon="#drawable/ic_game"
android:title="Game"/>
<item
android:id="#+id/nav_home"
android:icon="#drawable/ic_home"
android:title="Home"/>
<item
android:id="#+id/nav_ranking"
android:icon="#drawable/ic_ranking"
android:title="Ranking"/>
<item
android:id="#+id/nav_user"
android:icon="#drawable/ic_user"
android:title="User"/>
Related
So this is the code I have for my bottom navigation bar, in my tabs I'm retrieving data from parse
my question is what do I put inside my BottomNavigationView.OnNavigationItemReselectedListener in order for the tab that I reselect to reload/refresh
would very much appreciate any help!
final Fragment fragment1 = new HomeFragment();
final Fragment fragment2 = new UsersFragment();
final Fragment fragment3 = new ProfileFragment();
final Fragment fragment4 = new SettingsFragment();
final FragmentManager fm = getSupportFragmentManager();
Fragment active = fragment1;
#Override
public void onBackPressed() {
if(ParseUser.getCurrentUser() != null){
Intent a = new Intent(Intent.ACTION_MAIN);
a.addCategory(Intent.CATEGORY_HOME);
a.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(a);
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_home);
BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(navigationItemSelectedListener);
bottomNavigationView.setOnNavigationItemReselectedListener(navigationItemReselectedListener);
fm.beginTransaction().add(R.id.fragment_container, fragment4, "4").hide(fragment4).commit();
fm.beginTransaction().add(R.id.fragment_container, fragment3, "3").hide(fragment3).commit();
fm.beginTransaction().add(R.id.fragment_container, fragment2, "2").hide(fragment2).commit();
fm.beginTransaction().add(R.id.fragment_container,fragment1, "1").commit();
}
private BottomNavigationView.OnNavigationItemSelectedListener navigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull #NotNull MenuItem item) {
switch (item.getItemId()){
case R.id.nav_home:
fm.beginTransaction().hide(active).show(fragment1).commit();
active = fragment1;
return true;
case R.id.nav_list:
fm.beginTransaction().hide(active).show(fragment2).commit();
active = fragment2;
return true;
case R.id.nav_profile:
fm.beginTransaction().hide(active).show(fragment3).commit();
active = fragment3;
return true;
case R.id.nav_settings:
fm.beginTransaction().hide(active).show(fragment4).commit();
active = fragment4;
return true;
}
return false;
}
};
private BottomNavigationView.OnNavigationItemReselectedListener navigationItemReselectedListener = new BottomNavigationView.OnNavigationItemReselectedListener() {
#Override
public void onNavigationItemReselected(#NonNull #NotNull MenuItem item) {
}
};
}```
So you are calling the fragment through bottom navigation bar
So for every selection of an item in the bottom navigation yo do want to refresh the fragment
Do use attach() and detach() method while beginning the transaction. It refreshes each time
I'm trying to load all the fragments on the app start and they will never be reloaded even when the user navigates to different pages. Is it possible? The following is my MainActivity.java file.
Any help would be really appreciated.
public class MainActivity extends AppCompatActivity {
private WebView mywebView;
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
bottomNav.setOnNavigationItemSelectedListener(navListener);
//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 HomeFragment()).commit();
}
}
private BottomNavigationView.OnNavigationItemSelectedListener navListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()){
case R.id.nav_home:
selectedFragment = new HomeFragment();
break;
case R.id.nav_inbox:
selectedFragment = new InboxFragment();
break;
case R.id.nav_orders:
selectedFragment = new OrdersFragment();
break;
case R.id.nav_notifications:
selectedFragment = new NotificationsFragment();
break;
case R.id.nav_account:
selectedFragment = new MyaccountFragment();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
selectedFragment).commit();
return true;
}
};
}
EDIT:
This is one of my fragment: NotifcationFragment. Other fragments are almost similar to this one.
public class NotificationsFragment extends Fragment {
String ShowOrHideWebViewInitialUse = "show";
private WebView myWebView ;
private ProgressBar spinner;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View myView = inflater.inflate(R.layout.fragment_notifications,container,false);
myWebView = myView.findViewById(R.id.WebView3);
spinner = myView.findViewById(R.id.progressBar3);
myWebView.loadUrl("example.com/notifications");
myWebView.setWebViewClient(new CustomWebViewClient()
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return false;
}
}
);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setDomStorageEnabled(true);
myWebView.setOverScrollMode(WebView.OVER_SCROLL_NEVER);
return myView;
}
// This allows for a splash screen
// (and hide elements once the page loads)
private class CustomWebViewClient extends WebViewClient {
#Override
public void onPageStarted(WebView webview, String url, Bitmap favicon) {
// only make it invisible the FIRST time the app is run
if (ShowOrHideWebViewInitialUse.equals("show")) {
webview.setVisibility(myWebView.INVISIBLE);
}
}
#Override
public void onPageFinished(WebView view, String url) {
ShowOrHideWebViewInitialUse = "hide";
spinner.setVisibility(View.GONE);
view.setVisibility(myWebView.VISIBLE);
super.onPageFinished(view, url);
view.loadUrl("javascript:(function() { " +
"var foot = document.getElementsByClassName('footer-wrapper')[0].style.display='none'; " +
"var arrow = document.getElementsByClassName('scroll-to-top-wrapper')[0].style.display='none'; " +
"var head = document.getElementsByClassName('new-menu responsive')[0].style.display='none'; " +
"})()");
}
}
}
The way you have implemented when the user switch between the navigation bar tabs it always create new fragments. Then it should be refresh. If you don't want to refresh each time user navigate between tabs you need to change the code as follows:
Define Fragments creation at the top in the class
Eg :
private HomeFragment homeFragment = new HomeFragment(); (do for other fragments also)
Then in navigation view listener do as
> case R.id.nav_home:
> selectedFragment = homeFragment;
> break;
do for other fragments too
public class MainActivity extends AppCompatActivity {
private WebView mywebView;
private HomeFragment homeFragment = new HomeFragment();
private InboxFragment inboxFragment = new InboxFragment();
private OrdersFragment ordersFragment = new OrdersFragment();
private NotificationsFragment notificationsFragment = new NotificationsFragment();
private MyaccountFragment myAccountFragment = new MyaccountFragment();
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
bottomNav.setOnNavigationItemSelectedListener(navListener);
//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 HomeFragment()).commit();
}
}
private BottomNavigationView.OnNavigationItemSelectedListener navListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()){
case R.id.nav_home:
selectedFragment = homeFragment;
break;
case R.id.nav_inbox:
selectedFragment = inboxFragment;
break;
case R.id.nav_orders:
selectedFragment = ordersFragment;
break;
case R.id.nav_notifications:
selectedFragment = notificationsFragment;
break;
case R.id.nav_account:
selectedFragment = myAccountFragment;
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
selectedFragment).commit();
return true;
}
};
}
home fragment always on screen.i cant solve it.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNavigationView);
bottomNavigationView.setOnNavigationItemSelectedListener(this);
bottomNavigationView.setSelectedItemId(R.id.homeFragment)
}
/*#Override
protected void onStart() {
if (currentUser==null){
Intent welcomeIntent= new Intent(MainActivity.this,WelcomeActivity.class);
startActivity(welcomeIntent);
finish();
}
super.onStart();
}*/
homeFragment homeFragment = new homeFragment();
notificationFragment notificationFragment = new notificationFragment();
todolistFragment todolistFragment = new todolistFragment();
profileFragment profileFragment = new profileFragment();
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.homeFragment:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment, homeFragment).commit();
return true;
case R.id.notificationFragment:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment, notificationFragment).commit();
return true;
case R.id.todolistFragment:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment, todolistFragment).commit();
return true;
case R.id.profileFragment:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment, profileFragment).commit();
return true;
}
return false;
}
}
on home fragment it doubles.background texts are doesn't disappear.how can i solve it.what is the problem.i am using android studio with java
1
2
these are the screen shots.main text view always appears
I'm trying to have a navigation drawer that replaces a frame layout with a fragment on a navigation item click. Here's my code for the drawer...
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
mDrawerLayout.openDrawer(GravityCompat.START);
return true;
case R.id.things_manager:
ThingsManager thingsManager = new ThingsManager();
FragmentManager fragmentManager1 = getSupportFragmentManager();
fragmentManager1.beginTransaction().replace(R.id.fragment_container, thingsManager).commit();
return true;
case R.id.settings_tab:
Settings settings = new Settings();
FragmentManager fragmentManager2 = getSupportFragmentManager();
fragmentManager2.beginTransaction().replace(R.id.fragment_container, settings).commit();
return true;
case R.id.sign_out_tab:
Intent i = new Intent(this, Login.class);
startActivity(i);
return true;
}
return super.onOptionsItemSelected(item);
}
The nav menu pops up when I click the menu icon, but none of the item clicks work. Not even the sign out intent that I have at the bottom.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
android.support.v4.app.Fragment myFragment = null;
Class fragmentClass = null;
switch (item.getItemId()) {
case android.R.id.home:
//Example if your fragment activity name is fragment_home
fragmentClass = fragment_home.class;
break;
case R.id.things_manager:
//Same as Home
break;
case R.id.settings_tab:
//
break;
case R.id.sign_out_tab:
//
break;
}
try{
myFragment = (android.support.v4.app.Fragment) fragmentClass.newInstance();
}catch(Exception e){
e.printStackTrace();
}
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
//Back Button Wont Kill
fragmentManager.beginTransaction().replace(R.id.frameLayout, myFragment).addToBackStack(null).commit();
return super.onOptionsItemSelected(item);
}
Seeing that you have Drawer already implemented I assume you have already declared NavigationView in your Activity. If haven't already.
Implement OnNavigationItemSelectedListener in your Activity.
public class MyActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {....}
Add this in your onCreate of Activity
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
Add this method in your Activity to handle clicks on Drawer items.
#Override
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
mDrawerLayout.openDrawer(GravityCompat.START);
return true;
case R.id.things_manager:
ThingsManager thingsManager = new ThingsManager();
FragmentManager fragmentManager1 = getSupportFragmentManager();
fragmentManager1.beginTransaction().replace(R.id.fragment_container, thingsManager).commit();
return true;
case R.id.settings_tab:
Settings settings = new Settings();
FragmentManager fragmentManager2 = getSupportFragmentManager();
fragmentManager2.beginTransaction().replace(R.id.fragment_container, settings).commit();
return true;
case R.id.sign_out_tab:
Intent i = new Intent(this, Login.class);
startActivity(i);
return true;
}
mDrawerLayout.closeDrawer(GravityCompat.START);
return true;
}
Hi i'm making app where i'm using bottom navigation to navigate through the fragments but my problem is when i click the app is loaded fragments are not visible in frame layout and even when i click bottom navigation it is just showing blank page
below is my code for main activity
I searched for this problem on google but looks like i'm the only one having this problem
public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener{
FrameLayout frameLayout;
FragmentManager fragmentManager;
Fragment fragment;
BottomNavigationView bottomNavigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentManager = getSupportFragmentManager();
if(findViewById(R.id.frame_container)!=null){
if(savedInstanceState!=null){
return;
}
}
bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_nav);
//bottomNavigationView.setOnNavigationItemSelectedListener(this);
//final SeekBar sb = (SeekBar) findViewById(R.id.sb);
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()){
case R.id.navigation_home:
fragment = new HomeFragment();
loadFragment(fragment);
return true;
case R.id.navigation_feed:
fragment = new FeedFragment();
loadFragment(fragment);
return true;
case R.id.navigation_event:
fragment = new EventsFragment();
loadFragment(fragment);
return true;
case R.id.navigation_nearby:
fragment = new NearbyFragment();
loadFragment(fragment);
return true;
case R.id.navigation_profile:
fragment = new ProfileFragment();
loadFragment(fragment);
return true;
}
return false;
}
private void loadFragment(Fragment fragment) {
// load fragment
//HomeFragment homeFragment = new HomeFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container, fragment,null);
// transaction.addToBackStack(null);
/* Comment this line and it should work!*/
//transaction.addToBackStack(null);
transaction.commit();
}
}
and my xml
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:fillViewport="true"
android:fitsSystemWindows="true"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="0dp"
app:menu="#menu/navigation"
android:backgroundTint="#color/nav_color"
app:itemIconTint="#color/bottom_nav_fore_color"
android:background="?android:attr/windowBackground"
android:layout_weight="0.1">
</android.support.design.widget.BottomNavigationView>
</LinearLayout>
Your code seems correct , uncomment this line and then check :
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();
break;
case R.id.action_item2:
selectedFragment = ItemTwoFragment.newInstance();
break;
case R.id.action_item3:
selectedFragment = ItemThreeFragment.newInstance();
break;
}
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container, selectedFragment);
transaction.commit();
return true;
}
});
Hope this helps !
Remove this :
implements BottomNavigationView.OnNavigationItemSelectedListener
You don't set Your setOnNavigationItemSelectedListener means you are not setting fragment to bottom navigation drawer that why no fragment is viewing.
First add setOnNavigationItemSelectedListener to bottomnavigation
bottomNavigationView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
Then add this code-:
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment=null;
switch (item.getItemId()) {
case R.id.navigation_home:
fragment = new HomeFragment();
loadFragment(fragment);
return true;
case R.id.navigation_feed:
fragment = new FeedFragment();
loadFragment(fragment);
return true;
case R.id.navigation_event:
fragment = new EventsFragment();
loadFragment(fragment);
return true;
case R.id.navigation_nearby:
fragment = new NearbyFragment();
loadFragment(fragment);
return true;
case R.id.navigation_profile:
fragment = new ProfileFragment();
loadFragment(fragment);
return true;
}
return false;
}
};