I have a BottomnavigationView in an AppCompatActivity that navigates between Fragments. For one of the Fragments, it's a MasterDetailFragment where you can search in that fragment and it will filter the data in the MasterFragment and you can click on the list to view the DetailFragment (all of this happens while the user is on one of the tabs in BottomNavigationView). I'm trying to use the new arch ViewModel to share the data between Master and Detail fragments: https://medium.com/#bharathkumarbachina/sharing-data-between-fragments-34afb6553380. It says to use getActivity() and share the viewmodel that way, but getActivity() is not getting the AppCompatActivity but instead is getting a FragmentActivity. Do I need to make a new MasterDetailFragmentActivity?
AppCompatActivity is a subclass of FragmentActivity. You can cast the FragmentActivity to AppCompatActivity after doing "instance of" check.
https://developer.android.com/reference/android/support/v7/app/AppCompatActivity.html
Related
My main activity extends AppCompatActivity where I create and inflate my menu. I also want my main activity to contain a map fragment. This requires that my main activity extends FragmentActivity, but I cannot extend from two super classes. How am I supposed to add a map fragment to my main activity?
AppCompatActivity extends FragmentActivity, so you get all of the functionality in FragmentActivity by extending AppCompatActivity.
Well, AppCompatActivity already extends FragmentActivity, so there should be no problem.
I have 5 Fragments with basically the same code except for the onCreateLoader override. Is there a way to move the code that all my fragments share inside the Main Activity ?
Create BaseFragment which will contain your common code. After that extend your fragments from BaseFragment like this:
public class MyFragment extends BaseFragment
I'm using AppCompatPreferenceActivity, that is an Activity which extends PreferenceActivity and has an AppCompatDelegate. I want to add a headless fragment to this Activity, but I can't call to getSupportFragmentManager...
Is there a way to add fragments to a PreferenceActivity using AppCompatDelegate?
The only way to call getSupportFragmentManager is from a FragmentActivity or something which derives from it. PreferenceActivity derives from Activity which cannot use Fragments.
You should look into using a PreferenceFragment instead.
I'm creating a swipeable Sherlock tab. I used this tutorial to do it:
Android ActionBarSherlock ViewPager Tabs Tutorial
However when I want to start an activity from inside of a fragment (for example FragmentTab1) I need context to create intent and start activity.
I'm in the Fragment class and don't have access to context!
I can't use getActivity() too, because FragmentTab1 is created inside of getItem() function in FragmentPagerAdapter class.
What should I do?!
Sherlock Fragments works with getSherlockActivity() instead of getActivity()
You should try using
getSherlockActivity().startActivity(...)
I'm in the Fragment class and don't have access to context!
Yes, you do. Call getActivity() to return the Activity that is hosting this fragment. Activity inherits from Context.
I can't use getActivity() too, because FragmentTab1 is created inside of getItem() function in FragmentPagerAdapter class.
So? That does not somehow magically cause the getActivity() method to vanish.
For some reason, you are using ActionBarSherlock, which has been deprecated for about 20 months by its author. If your fragment is a SherlockFragment, you will want to call getSherlockActivity() instead.
I have an android application with a main activity extending a listactivity.
public class Main_activity extends ListActivity {...}
Out of the options menu, I want to send a part of the items via mail. To select the items, I want to display a dialogfragment.
Everything works fine, but I have to start a new intent (loosing my listview), this extending FragmentActivity, as it is not possible to use getSupportFragmentManager out of the ListActivity.
startActivity (new Intent (this, Fragment_Activity.class));
and
public class Fragment_Activity extends FragmentActivity implements EditNameDialogListener {...}
Is there any possibility to display the DialogFragment directly from my Main_activity? What do I have to change?
I would suggest that you change Main_activity to extend FragmentActivity and move your list code to a ListFragment. With your main activity extending FragmentActivity it gives you the chance to show the dialog fragment without losing the ListView and the underlying data.
UPDATE: Since you don't want to change your current design I see two ways forward:
1 Move the list data out of the ListActivity and into a data model of some kind so that you don't lose it. Create a static class to hold the list data or store it in SharedPreferences.
2 Use a custom dialog instead of a DialogFragment