-2

I want to start a fragment from B activity but the fragment is in main activity. If I use FragmentTransaction, but it gives error "No view found for ID for fragment"

Code

FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.layoutContent, frag);
ft.commit();

Error

No view found for id 0x7f0e00be (com.company.app:id/layoutContent) for fragment PlaylistFrag{5764566 #0 id=0x7f0e00be}

msanford
  • 11,803
  • 11
  • 66
  • 93
Ganesh Sumb
  • 59
  • 2
  • 8

5 Answers5

4

If I understand your question, You want to start a fragment in an Activity from another Activity. This is what I do to get Around that.

  1. From the current Activity, I would start the other Activity on Click

    Intent intent = new Intent (ActivityA.this, ActivityB.class);
    intent.putExtra("EXTRA", "openFragment");
    startActivity(intent);
    
  2. In the destination Activity, listen for intent extras and start action.

    switch (getIntent().getStringExtra("EXTRA")){ 
        case "openFragment":
            getSupportFragmentManager().beginTransaction().replace(R.id.replacableLayout, new FragmentActivityB()).commit();
            getSupportActionBar().setTitle("Fragment Activity B");
            break;
    }
    

It works for me...

0

this will fire an exception as getSupportFragmentManager() is a function with the Activity you work on it so when you use ft.replace(R.id.layoutContent, frag); will look for layoutContent on current activity and will not found

i don't how u want to

Solution ==> - use Event Bus or Rxjava :) or may use a n interface with setter and getter and check it in another activity or use static variable to check the fragment you want to replaced to :)

ahmed shaaban
  • 116
  • 1
  • 3
-1

Add this code in your onClick

// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();

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

// Commit the transaction
transaction.commit();

and add onBackPressed()

@Override
public void onBackPressed() {
    if (getFragmentManager().getBackStackEntryCount() > 0) {
        getFragmentManager().popBackStack();
    } else {
        super.onBackPressed();
    }
}
Athar Iqbal
  • 458
  • 3
  • 12
  • You can also use a sections pager adapter to achieve this as in my answer here https://stackoverflow.com/a/55722092/3904109 – DragonFire Apr 17 '19 at 07:29
-1

Make Layout like below in MainActivity.java and its layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_add_workout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.nujster.activity.AddWorkoutActivity">

    <LinearLayout
        android:id="@+id/add_workout_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"/>

</RelativeLayout>

Then, in main activity write below code to call fragment

public class AddWorkoutActivity extends AppCompatActivity {

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

            getSupportFragmentManager().beginTransaction().add(R.id.add_workout_fragment, new LevelFragment(), "levels").addToBackStack(null).commit();
    }

    @Override
    public void onBackPressed() {
        if (getSupportFragmentManager().findFragmentByTag("levels") != null) {
            LevelFragment levelFragment = (LevelFragment) getSupportFragmentManager().findFragmentByTag("levels");
            if (levelFragment.isVisible()){
               finish();
            } else{
                getSupportFragmentManager().popBackStack();
            }
        }
    }
}
SWAPDROiD
  • 357
  • 3
  • 15
-1

You can load same fragment in different activities.

Usually Fragment loads in Activity container.

Activity-1 layout have a container say R.id.layoutContent Activity-2 layout also should have a container R.id.xxx

ft.replace(R.id.layoutContent[Container where fragment loads], frag);

May be Activity-2 do not have id of container

Jeelan
  • 173
  • 1
  • 11