RecyclerView not displayed after changing fragment - android

I have the following situation: an activity with three fragments that I switch between them, in the fragment A for example I fetch some data, save it into an array and I set a recycler view using that array. Then I go to some other fragment, B, and when turning back to A the recycler view is not displayed anymore.
How can I solve this situation? I tried to put in the onResume method of the fragment mAdapter.notifyDataSetChanged() but it didn't worked.
How I swap the fragments:
mMainNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.navigation_home:
setFragment(homeFragment);
return true;
case R.id.navigation_search:
setFragment(searchFragment);
return true;
case R.id.navigation_notifications:
setFragment(notificationsFragment);
return true;
}
return false;
}
});
private void setFragment(Fragment fragment) {
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_frame, fragment);
fragmentTransaction.commit();
}

Related

(Android) NavigationBarView clear GridView

I have three fragment in NavigationBarView.
First fragment has gridview(gridview data is dynamic by network search).
But when I go to second fragment and back to first fragment, the gridview in first fragment is clear.
How can I prevent the gridview's data from being cleared?
Here my navigation code.
// in global variable
NavigationBarView navigationBarView;
FirstFragment firstFragment;
SecondFragment secondFragment;
ThirdFragment thirdFragment;
// ...
firstFragment = new FirstFragment();
secondFragment= new SecondFragment();
thirdFragment = new ThirdFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.containers, firstFragment).commit();
navigationBarView = findViewById(R.id.bottom_navigationview);
navigationBarView.setKeepScreenOn(true);
navigationBarView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch(item.getItemId())
{
case R.id.menu_first:
getSupportFragmentManager().beginTransaction().replace(R.id.containers, firstFragment).commit();
return true;
case R.id.menu_second:
getSupportFragmentManager().beginTransaction().replace(R.id.containers, secondFragment).commit();
return true;
case R.id.menu_third:
getSupportFragmentManager().beginTransaction().replace(R.id.containers, thirdFragment).commit();
return true;
}
return false;
}
});
addToBackStack method when replacing one fragment by another:
getFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).addToBackStack("my_fragment").commit();
and also use onbackpressed to back with state using back button
#Override
public void onBackPressed() {
if (getParentFragmentManager().getBackStackEntryCount() > 0) {
getParentFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}
use same popBackStack() to navigate from navigationbar also

OnBackPressed() is not working in activities

In my project, there are three fragments on the dashboard activity ( the default is selected as a home fragment) with a bottom navigation bar. In the Second fragment(connection fragment), there is a recycler view and when the items are clicked, an activity will be open.
The problem is after opening the activity when I click the back button (I have implemented an image button to go back), it goes to the home fragment, not the connections fragment which has the recycler view.Here is the implemented code.
backBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onKeyUp(int,KeyEvent);
}
});
#Override
public boolean onKeyUp(int keyCode, KeyEvent objEvent) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
onBackPressed();
return true;
}
return super.onKeyUp(keyCode, objEvent);
}
#Override
public void onBackPressed() {
finish();
}
Can someone explain what is the issue.I want to go back to the connection fragment.My bottom navigation bar code as follows,
private BottomNavigationView.OnNavigationItemSelectedListener selectedListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.navigation_discover:
DiscoverFragment fragment1=new DiscoverFragment();
FragmentTransaction ft1=getSupportFragmentManager().beginTransaction();
ft1.replace(R.id.content,fragment1 ,"");
ft1.commit();
return true;
case R.id.navigation_places:
PlacesFragment fragment2=new PlacesFragment();
FragmentTransaction ft2=getSupportFragmentManager().beginTransaction();
ft2.replace(R.id.content,fragment2 ,"");
ft2.commit();
return true;
case R.id.navigation_connections:
ConnectionFragment fragment3=new ConnectionFragment();
FragmentTransaction ft3=getSupportFragmentManager().beginTransaction();
ft3.replace(R.id.content,fragment3 ,"");
ft3.commit();
return true;
case R.id.navigation_profile:
ProfileFragment fragment4=new ProfileFragment();
FragmentTransaction ft4=getSupportFragmentManager().beginTransaction();
ft4.replace(R.id.content,fragment4 ,"");
ft4.commit();
return true;
}
return false;
}
};

Opening an unlisted fragment on a viewpager navigation?

I'm coupling BottomNavigationView with ViewPager, it works ok, the main reason for that is the swiping and animation.
There are 3 possibilities on my listener:
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
viewPager.setCurrentItem(0);
return true;
case R.id.navigation_solicitation:
viewPager.setCurrentItem(1);
return true;
case R.id.navigation_my_profile:
viewPager.setCurrentItem(2);
return true;
}
return false;
}
};
However, there are other, unlisted fragments. How can I inflate fragments on the view pager and make it go away once swiped or something else was selected?
Solution
Added this below the viewpager in the main layout file:
<FrameLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</FrameLayout>
Below is the method used to open the new fragment and make viewpager gone:
viewPager.setVisibility(viewPager.GONE);
fragment = new PostSolicitationFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_up, R.anim.slide_out_down);
ft.replace(R.id.content, fragment);
ft.commit();
And this is the new onNavigationSelectedItems to make it show up again in case the user clicks a menu item.
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
viewPager.setVisibility(VISIBLE);
viewPager.setCurrentItem(0);
return true;
case R.id.navigation_solicitation:
viewPager.setVisibility(VISIBLE);
viewPager.setCurrentItem(1);
return true;
case R.id.navigation_my_profile:
viewPager.setVisibility(VISIBLE);
viewPager.setCurrentItem(2);
return true;
}
return false;
}

Handle back-press inside of Fragments irrespective of navigation route

My app has a navigation drawer with three items linking to three fragments A, B and C
when I click on the back button from any of the Fragments, the app exits.I solved this by declaring these constants in my base activity:
public static boolean IS_FRAGMENT_A = false;
public static boolean IS_FRAGMENT_B = false;
public static boolean IS_FRAGMENT C = false;
and then in the selectView method I did this:
private void displayView(int position) {
IS_FRAGMENT_A = false;
IS_FRAGMENT_B = false;
IS_FRAGMENT_C = false;
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new FragmentA();
IS_FRAGMENT_A = true;
break;
case 1:
fragment = new FragmentB();
IS_FRAGMENT_B = true;
break;
case 2:
fragment = new FragmentC();
IS_FRAGMENT_C = true;
break;
default:
break;
}
And in the overriden onBackPressed() method I did this
public void onBackPressed() {
if (IS_FRAGMENT_A) {
finish();
} else {
displayView(0);
}
}
This works fine when am navigating to these fragments from the navigation drawer.However, when I go to any of the fragments through an ActionBar button, the onBackPressed method does not work.
In Fragment A, I have an ActionBar button that takes me to Fragment B:
public boolean onOptionsItemSelected(MenuItem menuItem) {
int id = menuItem.getItemId();
switch (id) {
case R.id.action_new:
Fragment fragmentB = new FragmentB();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame_container, fragmentB);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
return true;
default:
return super.onOptionsItemSelected(menuItem);
}
}
When I press the back button from FragmentB after navigating to it via this method the app exits, unlike when I navigate to it through the navigation drawer.
I have tried adding this to the onCreate() method of FragmentB with no luck:
BaseActivity.IS_FRAGMENT_B = true;
My question is, how do I make the back button work in all fragments irrespective of how I got there?

android menu item open new fragment

i'm using swiping tabs as navigation to display 5 pages ( fragments) , in the app menu ( 3 dot ) button i want to create 2 pages, 1 for settings, and other for about page ( just static text) . please advice how can i to this.
in specific, how can i go to a new fragment ( not in my tab) to be displayed when clicked from menu items.
please assist me, thank you in advance.
this is my code so far.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.action_reset:
Unsafe.uhs1a.setSelection(0);
Unsafe.uhs1b.setSelection(0);
Unsafe.uhs1c.setSelection(0);
Precondition.phs1a.setSelection(0);
Precondition.phs1b.setSelection(0);
Precondition.phs1c.setSelection(0);
case R.id.action_about:
// need to open a static page ( fragment) here
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Old question but WTH.You do this using a FragmentManager and her friend FragmentTransaction:
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.action_reset:
Unsafe.uhs1a.setSelection(0);
Unsafe.uhs1b.setSelection(0);
Unsafe.uhs1c.setSelection(0);
Precondition.phs1a.setSelection(0);
Precondition.phs1b.setSelection(0);
Precondition.phs1c.setSelection(0);
case R.id.action_about:
Fragment newFragment = new TheFragmentYouWantToOpen();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame_container, newFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
//frame_container is the id of the container for the fragment
return true;
default:
return super.onOptionsItemSelected(item);
}
Not sure if this is what you want, but I have a fragment which opens a new Actvity (with a fragment inside):
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case R.id.action_about:
Intent intent_to = new Intent(getActivity(),MyNewActivity.class);
startActivity(intent_to);
break;
}
return true;
}
Inside MyNewActivity class you should have a container and open a fragment using getFragmentManager()
You can even use startActivityForResult if you want to do something depending in how the new fragment closes
Hope it helps

Categories

Resources