user3079872
user3079872

Reputation: 191

Calling an Activity from Fragment

Apologies if this is a dumb question. I have searched for an answer to my problem and although I have found several mentions of this issue, none of the solutions proposed have solved my problem. In my app, I am calling an Activity from a Fragment and I then want to return to that Fragment via a button click in the Activity. 1 - In fragment Discover_Fragment I have a list view and when one item is pressed, the Activity EditThoughts starts:

 lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                                    int position, long arg3) {
                Intent intent1 = new Intent(getActivity(), EditThought.class);
                startActivity(intent1);
            }
        });

Activity EditThoughts starts with no issues. 2 - Then, when I want to return to the Fragment (Discover_Fragment) from Activity EditThought, I use the following:

                    FragmentManager fragmentManager = getSupportFragmentManager();
                    Discover_Fragment fragmentS1 = new Discover_Fragment();
                    fragmentManager.beginTransaction()
                            .replace(R.id.containerView, fragmentS1)
                            .commit();

I get an error:

java.lang.IllegalArgumentException: No view found for id 0x7f0e006e (com.appleia.vocapp:id/containerView) for fragment Discover_Fragment{f1e7d01 #0 id=0x7f0e006e}

R.id.containerView is the id for the FrameLayout in activity_main:

       <FrameLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/containerView">
       </FrameLayout>

The layout for Discover_Fragment is:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/toplayout"
    android:orientation="horizontal"

    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/topButtons"
        android:gravity="center"
        android:layout_weight="1">


        <com.appleia.SegmentedButtons.SegmentedRadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dip"
            android:orientation="horizontal"
            android:id="@+id/segment_text"
            android:checkedButton="@+id/button_one"
            android:gravity="center">
            <RadioButton android:id="@id/button_one"
                android:layout_height="wrap_content"
                android:layout_width="110dp"
                android:minWidth="110dp"
                android:minHeight="33dip"
                android:text="@string/Articles"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:button="@null"
                android:gravity="center"
                android:textColor="@drawable/radio_colors" />
            <RadioButton android:id="@+id/button_two"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:minWidth="0dp"
                android:minHeight="33dip"
                android:text="Two"
                android:button="@null"
                android:gravity="center"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="@drawable/radio_colors"
                android:visibility="gone"/>
            <RadioButton android:id="@+id/button_three"
                android:layout_height="wrap_content"
                android:layout_width="110dp"
                android:minWidth="110dp"
                android:minHeight="33dip"
                android:text="@string/Toughts"
                android:button="@null"
                android:gravity="center"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="@drawable/radio_colors" />
        </com.appleia.SegmentedButtons.SegmentedRadioGroup>

    </LinearLayout>

</LinearLayout>
<ListView
    android:layout_below="@+id/toplayout"
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

</RelativeLayout>

What am I doing wrong? Thanks in advance.

Upvotes: 1

Views: 119

Answers (4)

Satendra Baghel
Satendra Baghel

Reputation: 89

 lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1,
                                int position, long arg3) {
            Intent intent1 = new Intent(getActivity(), EditThought.class);
            getActivity().startActivity(intent1);
        }
    });

these lines of code will work properly so use it

Upvotes: 0

Meet Vora
Meet Vora

Reputation: 2818

The reason why you are getting the error:

when you want to return to the Fragment (Discover_Fragment) from Activity EditThought, you used the following:

                FragmentManager fragmentManager = getSupportFragmentManager();
                Discover_Fragment fragmentS1 = new Discover_Fragment();
                fragmentManager.beginTransaction()
                        .replace(R.id.containerView, fragmentS1)
                        .commit();

Here, in above your code, have a look at R.id.containerView, which is not from your current activity(EditThought Activity).

Now, Let's have a look at the solution:

As Alex Chengalan told, you have to just call onBackPressed() method instead of above code. And there you go!

Hope that helps!

Upvotes: 0

Alex Chengalan
Alex Chengalan

Reputation: 8281

You can startActivity by calling, startActivityForResult (Intent intent, int requestCode) docs here. After that you can return back to the activity by using onBackPressed. If you want to pass any data back to the Activity, then you can use setResult (int resultCode, Intent data) function for that. More information is here

Please note that if you want to get data back to the Activity class then you should call getActivity().startActivityForResult (Intent intent, int requestCode)

Or if you need to get data back to the fragment class itself just use startActivityForResult (Intent intent, int requestCode). It will return to onActivityResult in that fragment.

Upvotes: 1

Saurabh Vardani
Saurabh Vardani

Reputation: 1861

Do like this...

FragmentManager fragmentManager = getSupportFragmentManager();
                    Discover_Fragment fragmentS1 = new Discover_Fragment();
                    fragmentManager.beginTransaction()
                            .replace(R.id.containerView, fragmentS1).addToBackStack("fragBack");
                            .commit();

Description for addToBackStack - Add this transaction to the back stack. This means that the transaction will be remembered after it is committed, and will reverse its operation when later popped off the stack.

Upvotes: 0

Related Questions