Move code from Fragments into MainActivity - android

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

Related

Main activity with a menu and map fragment (Android app)

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.

getActivity() from Fragment is FragmentActivity not AppCompatActivity?

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

Android Fragment TextView setText from main activity

I got a MainActivity and it has 3 fragments. One of those fragments got one TextView. How Can I manipulate that TextView from MainActivity?
On my MainActivity I have a TabLayout with that 3 fragments. I got data from database in MainActivity and I would like to change the textview that is on fragment 1 with that data I got.
Thank you.
I am suggest you to create some method in one of your Fragment
example :
In Fragment One create/ add this method :
public void Test(String jajal_disek) {
Toast.makeText(getActivity().getApplicationContext(), jajal_disek,Toast.LENGTH_SHORT).show();
ABC.setText(jajal_disek); //example your textview name ABC
}
and then in MainActivity call that's method inside FragmentOne like this :
FragmentOne fragment = (FragmentOne) getSupportFragmentManager().findFragmentById(R.id.container_body);
fragment.Test("StackOverFlow");
Hope this will help you, be great if you write source code to identified problems

Using getSupportFragmentManager when using AppCompatDelegate?

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.

How to execute AsyncTask<> located in a fragment from its parent?

This seems harder than it look or I am doing it wrong. I need to update the content of UI elements in a fragment.
So, within an Activity, I start AsyncTask task like this
new MyAsyncTask().execute();
Inside "normal" Activity this async class is private so I thought that I could make it public inside the fragment class and then be able to call it from FragmentActivity parent class. Something like this:
MyFragment myFragment = (MyFragment) getSupportFragmentManager().findFragmentById(R.layout.my_fragment);
new myFragment.DoParsingAsyncTask().execute(""); //??? a silly guess
This did not work.
Can anyone help?
PS. My fragments are not <fragment> views in XML, but I deal with them via ViewPager. I have no problem displaying pages via ViewPager.
PPS. this async class starts parsing remote content and fill UI elements in the fragment.
It might not solve the problem, but create a method in your fragment and call that to create and start the async task. It will just be tidier. Here is the code.
From Fragment call the method like this.
Activity myActivity = getActivity();
if (myActivity instanceof MyActivity) {
((MyActivity) myActivity).doAsyncTaskMethod();
}
Or you can try the solution I've already proposed.
MyFragment myFragment = (MyFragment) getSupportFragmentManager().findFragmentById(R.layout.my_fragment);
myFragment.new DoParsingAsyncTask().execute("");
Which works as well.

Categories

Resources