Asked  7 Months ago    Answers:  5   Viewed   28 times

Trying to call a method in my activity from a fragment. I want the fragment to give the method data and to get the data when the method return. I want to achieve similar to call on a static method, but without the use of static because it create problems in the activity.

New to fragments so I need an easy and pedagogic explanation!




From fragment to activty:


From activity to fragment:

FragmentManager fm = getSupportFragmentManager();

//if you added fragment via layout xml
YourFragmentClass fragment = (YourFragmentClass)fm.findFragmentById(;

If you added fragment via code and used a tag string when you added your fragment, use findFragmentByTag instead:

YourFragmentClass fragment = (YourFragmentClass)fm.findFragmentByTag("yourTag");
Tuesday, June 1, 2021
answered 7 Months ago

Create the BroadcastReceiver dynamically:

In your BroadcastReceiver class define class member:

YourMainActivity yourMain = null;  

and method:

setMainActivityHandler(YourMainActivity main){
yourMain = main;

from your MainActivity do:

private YourBroadcastReceiverClassName yourBR = null;
yourBR = new YourBroadcastReceiverClassName();
    IntentFilter callInterceptorIntentFilter = new           IntentFilter("android.intent.action.ANY_ACTION");
    registerReceiver(yourBR,  callInterceptorIntentFilter);

finally, when yourBR.onReceive is fired you can call:

Friday, June 18, 2021
answered 6 Months ago

Ah, found it here

public class MainMenuHolder extends FragmentActivity {

    protected void onCreate(Bundle savedInstanceState) 
        // If not already added to the Fragment manager add it. If you don't do this a new Fragment will be added every time this method is called (Such as on orientation change)
        if(savedInstanceState == null)
            getSupportFragmentManager().beginTransaction().add(, new MainMenuFragment()).commit();

FragmentActivity allow's you to set the Fragment as the content of which I assume is the android internal ID of the trunk view.

With this method you still end up with an mostly redundant activity (If all you want is the Fragment acting as the Activity). But still, half as much fluff as having an activity and an XML file acting as a container.

Any other answers would be appreciated!

Saturday, September 11, 2021
answered 3 Months ago

Recently the is deprecated

It is not deprecated at the present time. For example, it is not marked as deprecated in the documentation.

'FragmentStatePagerAdapter(' is deprecated

The single-parameter FragmentStatePagerAdapter constructor is deprecated. However, if you read the documentation for that constructor, you will find:

This constructor is deprecated. use FragmentStatePagerAdapter(FragmentManager, int) with BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT

So, replace FragmentStatePagerAdapter(fm) with FragmentStatePagerAdapter(fm, FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT), to retain the functionality from the original one-parameter constructor.

Wednesday, October 6, 2021
Deepanshu Goyal
answered 2 Months ago
public static final int SERVICES_ALL = 1;
public static final int SERVICES_FEATURED = 2;
public static final int SERVICES_NEW = 3;

should be

public static final int SERVICES_ALL = 0;
public static final int SERVICES_FEATURED = 1;
public static final int SERVICES_NEW = 2;

getItem is 0-based-index, which means that in your code, the case 0 of your Adapter never matches one, and in that case you are returning a null fragment causing the exception

Tuesday, October 12, 2021
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :