0

I have a Class1 extending ListFragment1.
I have a Class2 extending ListFragment2.

I'm currently passing an Intent like this inside OnItemClickListener.

            Intent intent = new Intent(getActivity().getApplicationContext(), ListFragment2.class);
            intent.putExtra("id", "1");
            getActivity().startActivity(intent);

        }

However, i get this exception:

java.lang.ClassCastException: com.abc.xyz.Class2 cannot be cast to android.app.Activity

Please help ! :(

nstCactus
  • 5,141
  • 2
  • 30
  • 41
LEE
  • 3,335
  • 8
  • 40
  • 70

4 Answers4

1

Intent must point towards a class extending android.app.Activity which is not a case with your code.

Instead of extending ListFragment2 externally, you can make an inner class extending ListFragment2 and pass the value.

Hope this helps.

Pankaj Sharma
  • 669
  • 5
  • 12
1

Both the fragments must communicate with the activity for communication between each other.The activity acts as a sort of a mediater between the fragement.The flow will look like this:

  1. Fragment1 passes the intent to Activity1 with certain parameters which helps Activity1 to identify that it needs to pass this intent to Fragment2.
  2. Activity1 on receiving the intent, passes this intent to Fragment 2.

How the fragment communicates with activity can be designed in following way mentioned here

To allow a Fragment to communicate up to its Activity, you can define an interface in the Fragment class and implement it within the Activity. The Fragment captures the interface implementation during its onAttach() lifecycle method and can then call the Interface methods in order to communicate with the Activity.

public class HeadlinesFragment extends ListFragment { OnHeadlineSelectedListener mCallback;

// Container Activity must implement this interface
public interface OnHeadlineSelectedListener {
    public void onArticleSelected(int position);
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    // This makes sure that the container activity has implemented
    // the callback interface. If not, it throws an exception
    try {
        mCallback = (OnHeadlineSelectedListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement OnHeadlineSelectedListener");
    }
}

...

} Now the fragment can deliver messages to the activity by calling the onArticleSelected() method (or other methods in the interface) using the mCallback instance of the OnHeadlineSelectedListener interface.

For example, the following method in the fragment is called when the user clicks on a list item. The fragment uses the callback interface to deliver the event to the parent activity.

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    // Send the event to the host activity
    mCallback.onArticleSelected(position);
}

Implement the Interface In order to receive event callbacks from the fragment, the activity that hosts it must implement the interface defined in the fragment class.

For example, the following activity implements the interface from the above example.

public static class MainActivity extends Activity implements HeadlinesFragment.OnHeadlineSelectedListener{ ...

    public void onArticleSelected(int position) {
        // The user selected the headline of an article from the HeadlinesFragment
        // Do something here to display that article
    }
}

Deliver a Message to a Fragment The host activity can deliver messages to a fragment by capturing the Fragment instance with findFragmentById(), then directly call the fragment's public methods.

For instance, imagine that the activity shown above may contain another fragment that's used to display the item specified by the data returned in the above callback method. In this case, the activity can pass the information received in the callback method to the other fragment that will display the item:

public static class MainActivity extends Activity implements HeadlinesFragment.OnHeadlineSelectedListener{ ...

    public void onArticleSelected(int position) {
        // The user selected the headline of an article from the HeadlinesFragment
        // Do something here to display that article

        ArticleFragment articleFrag = (ArticleFragment)
                getSupportFragmentManager().findFragmentById(R.id.article_fragment);

        if (articleFrag != null) {
            // If article frag is available, we're in two-pane layout...

            // Call a method in the ArticleFragment to update its content
            articleFrag.updateArticleView(position);
        } else {
            // Otherwise, we're in the one-pane layout and must swap frags...

            // Create fragment and give it an argument for the selected article
            ArticleFragment newFragment = new ArticleFragment();
            Bundle args = new Bundle();
            args.putInt(ArticleFragment.ARG_POSITION, position);
            newFragment.setArguments(args);

            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

            // Replace whatever is in the fragment_container view with this fragment,
            // and add the transaction to the back stack so the user can navigate back
            transaction.replace(R.id.fragment_container, newFragment);
            transaction.addToBackStack(null);

            // Commit the transaction
            transaction.commit();
        }
    }
}
rupesh jain
  • 3,410
  • 1
  • 14
  • 22
1

First you should know:

  • You are calling startActivity(intent) and your Intent is pointing to a Fragment.
  • You cannot pass intents to your Fragment like that since Fragments are not App Components.
  • Communcation between Fragments should be between the Activity holding them.

Best Solution:

Pass your data from Class1 to your Activity and then send it to Class2, this link provides you exactly this, no more or less code: http://developer.android.com/training/basics/fragments/communicating.html
It's a must read when you want to communicate between fragments.

Polvora
  • 26
  • 2
  • Yes. Its all about the basics. Now i'm clear about what to do ! :) Thanks :) God bless Stack overflow and all of you. – LEE Aug 10 '14 at 06:41
0

This is one of the way to achieve it, i use constructor to pass the data

FragmentOne.java

int myData=12;
FragmentManager manager = getActivity().getSupportFragmentManager();
Fragment frgObj=FragmentTwo.newInstance(myData);
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.container, frgObj,"FragmentTwo");
ft.addToBackStack(null);
ft.commit();

FragmentTwo.java

int myData;
public static FragmentTwo newInstance(int _myData){
FragmentTwo fragment = new FragmentTwo();
myData=_myData
return  fragment;
}

ALSO REFER -- this -- StackOVERFLOW POST

Community
  • 1
  • 1
Devrath
  • 42,072
  • 54
  • 195
  • 297