Nenco
Nenco

Reputation: 241

calling a method from a fragment inside a mainactivity

I've been trying to solve this problem for a few days now and still cant figure it out. I've checked many similiar topics about this but didnt get me what I need.

As you can see I'm adding the fragments dynamically:

  public void displayFragment1() {

    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.fragment_container, new Fragment_1(), "frag1");
    fragmentTransaction.commit();


}

public void displayFragment2() {

    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.fragment_container, new Fragment_2(), "frag2");
    fragmentTransaction.commit();


}

Here is the part that I cant figure out. I'm searching the fragments by tag and it seems that its not working since I'm always getting nullpointer exception or classexception. Toast just shows the value of the text that I entered so its sending the data from a fragment to the activity. so that part is fine. Any ideas guys???

 @Override
public void inputValue(String text) {

    if (text != null) {

        Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();

        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, new Fragment_2(), "frag2");
        fragmentTransaction.commit();

        FragmentManager fm = getSupportFragmentManager();
        Fragment_2 fragment = (Fragment_2) fm.findFragmentByTag("frag2");
        fragment.setChangeTo(text);


    } else {

        Toast.makeText(MainActivity.this, "didnt recieve the value", Toast.LENGTH_SHORT).show();

    }


}

activity main xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.neven.question_for_stackoverflow.MainActivity"
    android:background="@drawable/my_background">

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

</FrameLayout>

MainActivity:

public class MainActivity extends AppCompatActivity implements Fragment_1.sendData {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    displayFragment1();
}

fragment 1 xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:tools="http://schemas.android.com/tools"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         tools:context="com.example.neven.question_for_stackoverflow.Fragment_1"
         android:background="@drawable/my_background">

<!-- TODO: Update blank fragment layout -->
<TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment"/>
<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="fragment 1"
        android:id="@+id/textView" android:layout_gravity="center"/>
<EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/etInputID" android:layout_gravity="center" android:layout_marginTop="50dp"
        android:hint="enter text here" android:textColorHint="#e3ca0a"/>
<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send"
        android:id="@+id/bSendID" android:layout_gravity="center_horizontal|bottom"
        android:layout_marginBottom="130dp"/>

fragment 2 xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:tools="http://schemas.android.com/tools"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         tools:context="com.example.neven.question_for_stackoverflow.Fragment_2"
         android:background="@drawable/my_background">

<!-- TODO: Update blank fragment layout -->
<TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment"/>
<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="fragment 2 - change me"
        android:id="@+id/tvChangeMeID" android:layout_gravity="center"/>

Upvotes: 0

Views: 240

Answers (3)

ishmaelMakitla
ishmaelMakitla

Reputation: 3812

The issue seems to be that you are calling displayFragment1(); in your Activity's onCreate and yet you are expecting to find Fragment_2 which you are only setting/initializing in displayFragment2(); - you never call this method. So, please change your code in the onCreate method to call displayFragment2() like this:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    displayFragment2();
}

UPDATE: Please add the call getSupportFragmentManager().executePendingTransactions() after the commit statement and then try looking up the Fragment by TAG and see if this at least returns a Fragment object and not null. Your code will now look like:

...
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, new Fragment_2(), "frag2");
        fragmentTransaction.commit();
        getSupportFragmentManager().executePendingTransactions()

    Fragment_2 fragment = (Fragment_2) getSupportFragmentManager().findFragmentByTag("frag2");
    fragment.setChangeTo(text);

I hope this helps.

Upvotes: 2

Glenford Fernandes
Glenford Fernandes

Reputation: 547

The way to handle this is:

Fragment fragment = getActivity().getFragmentManager().findFragmentById(R.id.fragment_container); 
if (fragment instanceof    YourFragmentClass) {
fragment.setChangeTo(value);
}

Upvotes: 0

Seishin
Seishin

Reputation: 1477

You should declare a FrameLayout in your Activity's layout xml where the fragments you want will be inflated. Then use that FrameLayout's id when making a transaction instead of android.R.id.content.

Upvotes: 0

Related Questions