30

I want to launch a new fragment to view some data. Currently, I have a main activity that has a bunch of actionbar tabs, each of which is a fragment. So, within a tab fragment, I have a button, chartsButton. I have my onclicklistener all set for it, and here's the onClick method:

public OnClickListener chartsListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        Intent chartFragment = new Intent();
        startActivity(chartFragment);   
    }
};

Now, as I said, this listener is within a class that extends Fragment. So, I want to launch a new fragment (chartsFragment) via the intent to replace the whole screen. When the user clicks back, it'll bring them back to the tabs and main activity. Here's my chart fragment:

public class chartsFragment extends Fragment {

    public View onCreateView() {
        //LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState
        LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        return (inflater.inflate(R.layout.chartfragment, null));
    }
}

The current error I am dealing with: "android.content.ActivityNotFoundException: No Activity found to handle Intent { }". That's fine, I understand that I could use getActivity().startActivity(chartsFragment), but that results in the same error. I suppose what I am looking for here, is how do I launch an intent from within a fragment that results in opening a new fragment?

Davek804
  • 2,804
  • 4
  • 26
  • 55
  • [Look here for another way to call fragment from fragment ](https://stackoverflow.com/questions/14627829/call-fragment-from-fragment) – Abhishek Tulse Jan 09 '20 at 11:01

5 Answers5

60

The answer to your problem is easy: replace the current Fragment with the new Fragment and push transaction onto the backstack. This preserves back button behaviour...

Creating a new Activity really defeats the whole purpose to use fragments anyway...very counter productive.

@Override
public void onClick(View v) {
    // Create new fragment and transaction
    Fragment newFragment = new chartsFragment(); 
    // consider using Java coding conventions (upper first char class names!!!)
    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(); 
}

http://developer.android.com/guide/components/fragments.html#Transactions

slinden77
  • 3,378
  • 2
  • 37
  • 35
  • 2
    Its work only when both fragment hosted by same activity – androidXP Oct 20 '16 at 12:49
  • Maybe, but why is that noteworthy? I have only one activity in all my applications. That's kind of the whole point isn't it; limiting the number of activities? – slinden77 Oct 21 '16 at 06:23
23

You cannot open new fragments. Fragments need to be always hosted by an activity. If the fragment is in the same activity (eg tabs) then the back key navigation is going to be tricky I am assuming that you want to open a new screen with that fragment.

So you would simply create a new activity and put the new fragment in there. That activity would then react to the intent either explicitly via the activity class or implicitly via intent filters.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
RaB
  • 1,545
  • 13
  • 16
  • This worked perfectly. I just created a new class called Chart, had it extend Activity, placed it in the Manifest, and had it load the xml layout I had intended for a fragment. The back functionality works as I desired too. Thanks so much! – Davek804 Mar 22 '12 at 23:44
  • 4
    wow, this seems a bit over-the-top. Why not just replace the current fragment with a new one and push the current one onto the backstack?!?!?! http://developer.android.com/guide/components/fragments.html#Transactions – slinden77 Jul 19 '13 at 22:06
  • and sure you can open new Fragments! – slinden77 Jul 19 '13 at 22:11
6

Try this it may help you:

public void ButtonClick(View view) {
    Fragment mFragment = new YourNextFragment(); 
    getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, mFragment).commit();
}
Mattia Maestrini
  • 32,270
  • 15
  • 87
  • 94
Amit Jayaswal
  • 1,725
  • 2
  • 19
  • 36
1

try this:

in SecondActivity:

btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(this, FirstActivity.class);
            intent.putExtra("openFragment", "YourFragment");
            startActivity(intent);
        }
    });

In the FisrtActivity that contains the fragment:

if (getIntent().getStringExtra("openFragment").equals("YourFragment")){
            getSupportFragmentManager().beginTransaction().replace(R.id.containerView, new YourFragment()).commit();
        }
Idir Belmokhtar
  • 133
  • 1
  • 2
  • 6
0

Try this it may help you:

private void changeFragment(Fragment targetFragment){

    getSupportFragmentManager()
         .beginTransaction()
         .replace(R.id.main_fragment, targetFragment, "fragment")
         .setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
         .commit();

}
Walery Strauch
  • 6,792
  • 8
  • 50
  • 57
Gilad Brunfman
  • 3,452
  • 1
  • 29
  • 29