There seem to be many question related to this topic. When i read most of them i had one problem. Usually people try to make a fragment and then spawn a dialog fragment from there. So the dialog fragment is inside the fragment.
In my case i have created a button that opens a dialogfragment on the toolbar. Then i open my main fragment from the navigation bar. So the dialogfragment and my main fragment are getting called through the same main activity, just in different places. I cannot call the dialog fragment from inside the fragment since its getting called when the menu options are selected.
I tried to create a an interface with and a listener when im trying to
newFragment.setTargetFragment(somethinghere, 1); i have no idea on what i should be putting on the somethinghere field. getactivity,mealsworkoutfragment and .this are wrong)
I also tried to make my fragment extends Dialogfragment instead but this spawns my fragment everytime i press the button, which seems like an awful way to do this.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.date_picker) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.mealworkoutTab) {
iconShow = true;
invalidateOptionsMenu();
FragmentTransaction mealsworkoutsFrag = getSupportFragmentManager().beginTransaction();
mealsworkoutsFrag.replace(R.id.flMain,new MealsWorkoutsFragment());
mealsworkoutsFrag.commit();
} else if (id == R.id.userinfoTab) {
iconShow = false;
invalidateOptionsMenu();
FragmentTransaction userInfoFrag = getSupportFragmentManager().beginTransaction();
userInfoFrag.replace(R.id.flMain,new UserInfoFragment());
userInfoFrag.commit();
} else if (id == R.id.logoutTab) {
auth.signOut();
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
Im guessing that a simple solution must exist since this looks a bit trivial (get sth from a dialogfragment to a fragment)
A suggestion would be to use library like EventBus in such cases to convey and listen listen to "events" and pass data. So in your case, when the user selects the date, an 'event' with the selected date payload is posted. This event is then caught inside your Fragment (because you make your Fragment subscribe to this specific event) and then you can handle the payload (selected date) as you like.
If the above sounds too tough to follow or picture, please visit the link above. It has example codes. This library is extremely simple to implement and quite helpful in such scenarios.
Related
i want to know how to start all my activities in same intent. My app have a drawer activity and i want when i select any item, it will start an activity in the same intent, how can i do that ?
Main Acitivy
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
startActivity(new Intent(MainActivity.this, Main2Activity.class));
return true;
I checked Google Drive and I'm guessing you're referring to the behavior that when you click on something only a part of the screen updates. You can easily implement this by using Fragments. I cannot explain it to you in detail but this is a developer.google link that will help you get started. https://developer.android.com/training/basics/fragments/index.html
What function should i use to open an activity when a DrawerLayout navigationView item is clicked, and do i need to make object for that activity?
Here is my code so far:
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_profile) {
// Handle the camera action
} else if (id == R.id.nav_cv) {
} else if (id == R.id.nav_subscription) {
} else if (id == R.id.nav_logout) {
}
You can open activity like this.
Intent I=new Intent(MainActivity.this,actvitytoopen.class);
StartActivity(I);
You better use fragments instead of activities and change them using FragmentManager. You can find plenty of tutorials just by searching on Google/Youtube.
You can start new activity by
Intent intent = new Intent(this,NextActivity.class);
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
for further info refer https://developer.android.com/training/basics/firstapp/starting-activity.html
I spent a lot of time trying to call a new activity by clicking on navigation drawer item . but it is virtually impossible and if it is possible in any other way I tried, then it is sure that on the called activity there will be no drawer navigation drawer then. Fragment is a part of similar activity so calling fragment is the correct way. If you are calling fragment, navigation drawer will still be there on the side pane and you would be able to select between different items of the drawer.
I have an Activity that has a tabhost and a ViewPager that support 3 fragments. I want to use the ActionBar (which resides in the Activity) to use and modify menu items selectable from the ActionBar directly in each of the fragments.
What is the best way to send a message from the activity to the fragment to tell it to implement one of the actionbar items?
I have tried the following:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
FragmentManager manager = getSupportFragmentManager();
List<Fragment> fs = new ArrayList<>(manager.getFragments());
int fragId = fs.get(0).getId();
PlayFragment fragment = (PlayFragment) manager.findFragmentById(fragId);
fragment.settingsMenu();
return true;
This is not working, but also is bad form to need to know the order of the fragments to get it to work. This way didn't work for me. I know I could use a broadcastSender to make it work, but that is also bad form. The REAL problem here is that since the fragments are sitting on top of a ViewPager within a TabHost, there is no way to get the id or tag reliably, so
manager.findFragmentById(fragId) and
manager.findFragmentByTag("MyFragmentName") are not useful.
When I try to attach the toolBar to an OnClickListener within the fragment, it doesn't work, and fragment has no support for
#Override public boolean onOptionsItemSelected(MenuItem item);
I'll keep looking for a solution. Thanks.
I found the answer on another post. The trick is to get the toolbar from the activity but not to use it as an actionbar directly. You need to inflate your own menu, and have access to modifying title etc ;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.play_fragment, container, false);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
app = JamSessionApplication.getInstance();
toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
toolbar.setTitle(song);
// Set an OnMenuItemClickListener to handle menu item clicks
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_reverb) {
Toast.makeText(getActivity(), "Reverb", Toast.LENGTH_SHORT).show();
return true;
}
return true;
}
});
toolbar.inflateMenu(R.menu.menu_play);
}
I still encountered problems with ViewPager/Tabhost toobars working properly. I found an article which was much more helpful in fixing this.
https://www.numetriclabz.com/android-tabs-with-fragments-and-viewpager-tutorial/
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;
}
Android Studio 0.5.8
Hello,
I have one activity (MainActivity) that will host 2 fragments (ListAddressBookFragment, AddAddressBookFragment) (only one at a time). The initial fragment will be the ListAddressBookFragment and will be inflated when MainActivity onCreate gets called.
/* MainActivity.java */
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* Add and display fragment */
FragmentManager fragmentManager = getSupportFragmentManager();
Fragment fragment = fragmentManager.findFragmentById(R.id.flFragmentContainer);
/* Create new fragment if this hasn't already been done */
if(fragment == null) {
fragment = new ListAddressBookFragment();
fragmentManager.beginTransaction()
.add(R.id.flFragmentContainer, fragment)
.commit();
}
}
In the ListAddressBookFragment I have a option menu to add a new addressbook item. So this will call call MainActivity. So I want to replace ListAddressBookFragment with AddAddressBookFragment. However, because the code above is hardcoded I am wondering is there anyway to do this on the fly?
/* ListAddressBookFragment.java */
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.new_addressbook) {
Intent intent = new Intent(getActivity(), MainActivity.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
Many thanks for any suggestions,
I would recommend you to read this article, Communicating with fragments
Basically, you need to call
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newFragment);
Also, consider using interface within fragment that activity implements.
Why don't you pass data to the MainActivity to indicate the mode you want the activity to be in?
intent.putExtra("mode", "addressbook");
In the MainActivity, you do the below.
String mode = (String)getIntent().getStringExtra("mode");
if ("addressbook".equals(mode)) {
// Address book fragment
} else {
// ListAddressBookFragment
}
Good Luck