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
Related
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
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;
}
};
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();
}
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;
}
I'm using this tutorial to implement facebook login etc.
Facebook Login
I have added new fragment in this to show list of friends. Now when I press back button on my newly added fragment it takes me to the SPLASH fragment, I want same behaviour on back button on action bar. Means when I'm on my new fragment it shows me a back button on action bar. And pressing that back button takes me back to the SPLASH screen.
private void showFragment(int fragmentIndex, boolean addToBackStack) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
for (int i = 0; i < fragments.length; i++) {
if (i == fragmentIndex) {
transaction.show(fragments[i]);
} else {
transaction.hide(fragments[i]);
}
}
if (addToBackStack) {
transaction.addToBackStack(null);
}
transaction.commit();
}
You can do that by two ways :
1. Inside your fragment
#Override
public void onDetach()
{
super.onDetach();
PUT YOUR CODE HERE
}
This will called when fragment will be finished.
2. Just add addToBackStack while you are transitioning between your fragments like below:
fragmentManager.beginTransaction().replace(R.id.content_frame,fragment).addToBackStack("tag").commit();
if you write addToBackStack(null) , it will handle it by itself but if you give a tag , you should handle it manually.
EDITED:
for doing transactions
FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
Fragment scheduleFragment = new ScheduleFragment();
fragmentTransaction.replace(R.id.content_container, scheduleFragment, "scheduleFragment");
fragmentTransaction.addToBackStack("scheduleFragment");
fragmentTransaction.commit();
#Yawar actionbar is on activity only and this will added on activity it will be called evertime when u press actionbar home button-->
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; goto parent activity.
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
i got this code on stackoverflow after searching hard hope this may help you
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);// in on Create()
search for code onOptionsItemSelected(MenuItem item)
and edit it in this way
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// change your behaviour here
Intent intent = new Intent(this, yourclass.class);// i started new activity here
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}