-6

i want to replace activity to a fragment, this code is not working.

Intent intent = new Intent(Activity.this, Fragment.class);
startActivity(intent);
finish();
Sikander Bhutto
  • 226
  • 1
  • 11
  • 1
    No thats not a way .Post your total code .How many fragments there ?? – IntelliJ Amiya May 23 '16 at 09:45
  • 2
    Are you trying load fragment into activity? – Vilva May 23 '16 at 09:46
  • activity to fragment – Sikander Bhutto May 23 '16 at 09:47
  • 2
    `activity to fragment` means **nothing**. You can only have a Fragment **inside** an Activity. Please learn about Android Fragments. – Phantômaxx May 23 '16 at 10:05
  • *this code is not working*, that's un understatement. Why don't you do like everyone else, read the basics at least... Or at the very least use Google to search for similar questions. You probably have a friend who's savvy with computers, ask him how to make a Google search. Here's a secret hacker tip : Alt+F4 to solve compilations problems. – 2Dee May 23 '16 at 14:08

3 Answers3

1

You cannot switch from an Activity to Fragment, because a Fragment does not have its own existence without an Activity. i.e. a Fragment works inside an Activity.
Basically, Fragments are mainly used to create multi-pane screens.

Inside an Activity if you can replace Fragments (associated with the Activity) as mentioned in the above code examples to change the UI.

0

Try like this in your activity

@Override
public void replaceFragment(Fragment fragment, boolean addToBackStack) {

    FragmentTransaction transaction = getSupportFragmentManager()
            .beginTransaction();

    if (addToBackStack) {
        transaction.addToBackStack(null);

    } else {
        getSupportFragmentManager().popBackStack(null,
                FragmentManager.POP_BACK_STACK_INCLUSIVE);

    }
    transaction.replace(R.id.flContent, fragment);
    transaction.commitAllowingStateLoss();
    getSupportFragmentManager().executePendingTransactions();

}

and use like this

    YourFragment mYourFrag = new YourFragment ();
    replaceFragment(mYourFrag , false);
Abhishek Patel
  • 4,280
  • 1
  • 24
  • 38
0

Create a Fragmen class like

public class FragmentName extends android.support.v4.app.Fragment {}

And then you are able to cast a activity to view like:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View returner = null;
    Intent intent = new Intent([CONTEXT],[CLASSNAME.class]);
    Bundle args = this.getArguments();
        final Window w = [LocalActivityManager].startActivity("Title", intent);
        final View wd = w != null ? w.getDecorView() : null;

        if (wd != null) {
            ViewParent parent = wd.getParent();
            if(parent != null) {
                ViewGroup v = (ViewGroup)parent;
                v.removeView(wd);
            }

            wd.setVisibility(View.VISIBLE);
            wd.setFocusableInTouchMode(true);
            if(wd instanceof ViewGroup) {
                ((ViewGroup) wd).setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
            }
        }
    returner = wd;
    return returner;
}
2red13
  • 11,197
  • 8
  • 40
  • 52