David Silva
David Silva

Reputation: 67

Intent between 2 fragments not working

I'm using Fragments in my app. One of them has a listView, and when I do a setOnItemClickListener, I want to pass the item clicked to the other fragment... Here's the class OngletCours where I do my Intent:

   l1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
       @Override
       public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
           Intent  a = new Intent(getActivity(), OngletNotes.class);


           startActivity(a);
       }

   });


    return rootView;
}

I'm getting the following error when I try to do an Intent to go to another Fragment:

E/AndroidRuntime: FATAL EXCEPTION: main
              android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.dasilvadd.students/com.example.dasilvadd.students.OngletNotes}; have you declared this activity in your AndroidManifest.xml?

Here is my Manifest.xml

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="Student"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".PageAccueil">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Inscription"
        android:label="Student" />
    <activity
        android:name=".Onglets"

        android:label="Student"
        android:theme="@style/AppTheme.NoActionBar" />
    <activity android:name=".Reglages" />
    <activity android:name=".MotDePasse" />



    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

</application>

I've already tried to add it by myself, but it doesnt recognize the class I created for the Second Fragment (OngletNotes).

Please tell me how to solve this. And thank you in advance !

Upvotes: 0

Views: 318

Answers (3)

aldakur
aldakur

Reputation: 419

After readed your last comment I have edited my aswer:

Open the fragment:

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ((MainActivity)getActivity()).setCurrentItem (1, true);
        }
    });

In your MainActivity which contains ViewPager For example private ViewPager mViewPager; you need setCurrentItem method:

public void setCurrentItem (int item, boolean smoothScroll) {
    mViewPager.setCurrentItem(item, smoothScroll);
}

I suppose that OngleNotes fragment is the second fragment. That's why I send a 1 as a parameter.

Upvotes: 0

Alan
Alan

Reputation: 296

@ Update: OngletNotes is a fragment

Fragment example:

    public static OngletNotes newInstance(/* input parameters if any */) {
        OngletNotes fragment = new OngletNotes();
        // put values which you want to pass to fragment
        // Bundle args = new Bundle();
        // fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_ongletNotes, container, false);
        // if you have passed some data above
        // Bundle args = getArguments();
        return view;
    }

============================================================

@ Update: First, your onCreate in MainActivity,

// create a new instance of OngletCours (example is shown above)
OngletCours OngletCoursFragment = OngletCours.newInstance();
getSupportFragmentManager()
    .beginTransaction()
    .replace(R.id.fragment_holder, OngletCoursFragment )
    .commit();

// and your OnItemClickListener
l1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                OngletNotes targetFragment = OngletNotes.newInstance();
                FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
                transaction.addToBackStack(null);
                transaction.replace(R.id.fragment_holder, targetFragment)
                        .commit();
            }

        });

activity_main.xml: you shoud have a FrameLayout which is used as a container to switch fragments

<FrameLayout
        android:id="@+id/fragment_holder"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

Upvotes: 0

Vadims Savjolovs
Vadims Savjolovs

Reputation: 2668

If you start OngletNotes using startActivity, I assume it's an Activity. In that case you need to add it to the AndroidManifest.xml

<activity
    android:name=".OngletNotes"
    android:label="Notes" />

If OngletNotes is a Fragment, it should be put inside of the Activity. You can't launch standalone Fragment without an Activity.

  1. You need to create an Activity (don't forget to put it in AndroidManifest.xml)

  2. Put your Fragment inside of the Activity (in xml or programmatically)

  3. Start the Activity using startActivity

Upvotes: 2

Related Questions