I have an activity(say activity X) which contains a search box. On searching, I move on to another activity.( say Activity Y) I have used SherlockFragmentActivity to extend this Activity class and inside this activity I am loading a single fragment(say fragment A).
This fragment has many rows and on click of each row I am loading another fragment(say fragment B).
The problem is on clicking home button on my navigation bar from fragment B, Activity X is getting loaded and not fragment A.
I want fragment A to be loaded. How can I accomplish this?
What I have tried was: In my Activity Y,
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
#Override public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home :
this.getSupportFragmentManager().popBackStack();
return true;
}
return super.onOptionsItemSelected(item);
}
you can try like this.
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
newFragment=new FragementA();
FragmentTransaction transaction=getFragmentManager().beginTransaction();
transaction.replace(R.id.myFragement,newFragment);
transaction.addToBackStack(null);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.commit();
return true;
}
Related
I was trying to implement the bottom navigation with fragment inside an activity.
I have done that and it was successful.
To replace the fragment on click each navigation item, I use the following code.
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_notifications:
loadFragment(new NotificationsFragment());
return true;
case R.id.navigation_notes:
loadFragment(new NotesFragment());
return true;
case R.id.navigation_about:
loadFragment(new AboutFragment());
return true;
}
return false;
}
};
private void loadFragment(Fragment fragment) {
// load fragment
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
Now the problem is that when I click on a navigation in the bottom navigation bar multiple times, the fragment is also getting added to the back stack multiple times.
So when I click the back button the fragment is again loaded, not exiting or loading the previous fragment.
So, How can I prevent the fragment getting added to the back stack multiple times?
Remove
transaction.addToBackStack(null);
or add
transaction.disallowAddToBackStack();
I have only one activity in my application which contains navigation view to navigate through 3 fragments.
When I navigate to a fragment I get the hamburger menu icon changed to the up button in the onNavigationItemSelected method like this:
actionBarDrawerToggle.setDrawerIndicatorEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
The up button appears well but has no effect yet.
I wanna navigate to the previous fragment in the stack when the button is clicked but I can't get to add a listener to that button.
I've tried using:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
but this is never called. I think it's not called because I have no parent activity in the manifest.
Thanks in advance.
You should not call onBackPressed, you should use fragment transaction method to to transact between fragments like I replaced my current fragment with previous fragment using replace method. container is the layout containing the fragments.
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newFragment);
transaction.commit();
I hope, it give you some direction.
if (id == android.R.id.home) {
//put the code like above here.
return true;
}
I have a problem with developing an app for both tablets and smartphones with fragments.
When app is executed on smartphone, I show a fragment with a list (categories). When clicking on an item, I start a new activity that holds a ViewPager that inflates Fragments with another list (detail).
When app is executed on tablet I have a layout with two fragments inside. The left one is the categories list and the right one is the detail list.
Up to here there's no problem, but when setting onOptionsItemSelected I have problems.
When executing app on smartphone, everything is working well. This works:
Activity containing ViewPager
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.icShare:
sharePortal();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
Fragment inflated by ViewPager
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.icFavourite:
Utils.setFavouritePortal(mContext, mPortal);
getActivity().invalidateOptionsMenu();
return true;
case R.id.icShowFavourites:
showFavouriteArticles();
return true;
default:
break;
}
return super.onOptionsItemSelected(item);
}
But when executing the app in tablet, onOptionsItemSelected is not called. This is the code:
Fragment containing ViewPager
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.icShowFavourites:
Log.e("asdf", "asdfasdf - test tablet");
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
Fragment inflated by ViewPager
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.icFavourite:
Utils.setFavouritePortal(mContext, mPortal);
getActivity().invalidateOptionsMenu();
return true;
case R.id.icShowFavourites:
showFavouriteArticles();
return true;
default:
break;
}
return super.onOptionsItemSelected(item);
}
The only difference is that when executing on smartphone, the first onOptionsItemSelected is hold by an Activity and in tabled is hold by a fragment. I also tried to execute the onOptionsItemSelected from the Activity containing the fragment that contains the ViewPager inflating other fragments with no luck.
How can I get it working?
Thank you in advance!
Try to put setHasOptionsMenu(true); inside the fragment where you want onOptionsItemSelected() to be called.
Documentation
The fragment should call setHasOptionsMenu(true), and should implement onCreateOptionsMenu() and onOptionsItemSelected().
I'm using a navigation drawer in a main activity with many fragments
For example :
I have fragment 1 for presentation
And fragment2 that contains a listView.
If I click an item in the listView it will launch another activity
When i click the action bar back button i want to go to the main activity fragment2
But the fragment1 is used
How can I use the second fragment
Thanks for your answers
In your second activity's onCreate, put(although i think you already have) :
getActionBar().setDisplayHomeAsUpEnabled(true);
and inside second activity's options handling(i think you've done this too) :
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case android.R.id.home:
//perhaps use intent if needed but i'm sure there's a specific intent action for up you can use to handle
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
and inside your first activity's onCreate's intent handler for example :
if(Intent.ACTION_VIEW.equals(action))
you set the current page of the viewpager with viewpager.setCurrentItem(position). If you want to change the page for different items you could use intent.putExtra() in your second activity and then retrieve it in your first activity.
I have added the code below in my second activity :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
onBackPressed();
return true;
}
It's working as I expected
is it a clean solution ?
Thanks
Let's say I have a ListFragment A that is rooted from MainActivity A. User presses a list from List A and go to the FragmentActivity B. FragmentActivity holds 3 tabs of fragments.
So, I want to put an up navigation to the FragmentActivity B, so that it goes back to ListFragment A. How do I go about that?
This is my try, so far no luck:
public class ItemDetailActivity extends FragmentActivity implements ActionBar.TabListener {
...
actionBar.setDisplayHomeAsUpEnabled(true);
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
LatestFragment fragment = new LatestFragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.pager, fragment).addToBackStack(null)
.commit();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
LatestFragment is the ListFragment A I want to go back to.
However, I got an error that says I have to implement OnLatestSelectedListener because in LatestFragment, I already put an interface to pass values.
What else can I go inside onOptionsItemSelected?
Assuming you started ItemDetailActivity with a standard intent, you should just be able to use back action like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
super.onBackPressed();
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
If fragment A is in your main activity then you can also look in to using ActionBar. Back button takes you to the main activity. It is quite easy to implement as well.
I assume your FragmentActivityB has got the back button.
if not you can get it by
getActionBar().setDisplayHomeAsUpEnabled(true);
<meta-data android:name="android.support.PARENT_ACTIVITY"
android:value="#######" />
Now you can just identify fragments rooted with MainActivity using some flags like ViewNumber(1,2,3).
Implement the method onMenuItemSelected(....) and pass the viewNumber ie; the Fragment Which you want to show.
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
//We are implementing this method to respond to the back button on the action bar.
int itemId = item.getItemId();
switch (itemId) {
case android.R.id.home:
Intent i = new Intent(this,MainActivity.class);
i.putExtra("viewNumber",2);
startActivity(i);
break;
}
return true;
}//onMenuItemSelected
In MainActivity get the viewNumber using Bundles, and you can have some method like this to work out and display the desired Fragment.
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 1:
fragment = new ListFragmentB();
break;
case 2:
fragment = new ListFragmentA();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
}
}
}
I hope it will solve your problem.