Skip to content Skip to sidebar Skip to footer

Activity In Fragment Class

So i created a navigationDrawer and i have a problem I need to put my activity ActivitySec package com.calculatorvamal.app; import android.app.Activity; import android.os.Bundl

Solution 1:

I think that you have misunderstood or not read up on how to communicate between Fragments and Activities. You should read up on how to do so: https://developer.android.com/training/basics/fragments/communicating.html.

Also you should not mix your Fragment logic and your Activity logic. Actions to be performed with aspects or Views (such as button click listeners and list view creation etc) from your discreet Fragments and their layouts should be contained in the Fragment class. If you need to pass results from one to the other for various reasons then you can communicate between the separate entities like so:

Passing information from Activity to Fragment:

To send information to a fragment you need only define a method in the Fragment to receive it and call that from your Activity:

publicclassMyActivityextendsActivity
{
    privateMyFragment myFragment;

    @OverridepublicvoidonCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_holder);
        myFragment = newMyFragment();
        getSupportFragmentManager().beginTransaction()
            .add(R.id.fragment_container, myFragment).commit();
    }

    privatevoidtestFragmentCommunication(String message)
    {
        myFragment.recievedCommunication(message);
    }
}

publicclassMyFragmentextendsFragment
{
    ...

    publicbooleanrecievedCommunication(String message)
    {
        Log.d("MyFragment", "Recieved communication from Activity: " + message);
    }
    ...
}

Passing information from Fragment to Activity:

However if you need to pass information back from your Fragment to your Activity you will need to define an Interface for them to use:

publicclassMyFragmentextendsFragment
{
    MyActivityInterface activityInterface;

    @OverridepublicvoidonAttach(Activity activity)
    {
        super.onAttach(activity);
        try
        {
            activityInterface = (MyActivityInterface) activity;
        }
        catch (ClassCastException e)
        {
            Log.e(TAG, "Parent Activity deosn't implement 'MyActivityInterface'");
            thrownewClassCastException(activity.toString()
                    + " must implement MyActivityInterface");
        }
    }

    publicInterfaceMyAcyivityInterface
    {
        publicvoidonMessageReceived();
    }

    privatevoidtestActivityCommunication()
    {
        activityInterface.onMessageReceived(String message);
    }
}

publicclassMyActivityextendsActivityimplementsMyFragment.MyActivityInterface
{
    privateMyFragment myFragment;

    @OverridepublicvoidonCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_holder);
        myFragment = newMyFragment();
        getSupportFragmentManager().beginTransaction()
            .add(R.id.container, newPlaceholderFragment()).commit();
    }

    publicvoidonMessageReceived(String message)
    {
        Log.d("MyActivity", "Recieved communication from Fragment: " + message);
    }
}

Post a Comment for "Activity In Fragment Class"