Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using intent to call a fragment from another fragment

I am using a viewpager control to host three fragments.I have placed a button in each fragment to go to the next fragment.So i wanted to know as how to go from one fragment to another using intent?

The main purpose is to create a sign up form which has been divided into three fragments.The code is shown below:

The main activity:

package pl.looksok.viewpagerdemo;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class MainActivity extends FragmentActivity {
    ViewPager pager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MyPagerAdapter pageAdapter = new MyPagerAdapter(getSupportFragmentManager());
         pager = (ViewPager)findViewById(R.id.myViewPager);
        pager.setAdapter(pageAdapter);

    }

}

The first fragment:

package pl.looksok.viewpagerdemo;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.support.v4.app.Fragment;

public class FragmentBlue extends Fragment {
Button btnnext1;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_blue, container, false);
        btnnext1=(Button) view.findViewById(R.id.btnnext1);

        btnnext1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                 Intent intent = new Intent(view.getContext(), FragmentGreen.class);
                    view.getContext().startActivity(intent);
                    getActivity().finish();

            }
        });

        return view;
    }
}

The second fragment:

package pl.looksok.viewpagerdemo;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.support.v4.app.Fragment;

public class FragmentGreen extends Fragment {
Button btnnext2;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_green, container, false);
        btnnext2=(Button) view.findViewById(R.id.btnnext2);
        btnnext2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                Intent intent = new Intent(view.getContext(), FragmentPink.class);
                view.getContext().startActivity(intent);
                getActivity().finish();

            }
        });
        return view;
    }
}

The third fragment:

package pl.looksok.viewpagerdemo;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.support.v4.app.Fragment;

public class FragmentPink extends Fragment {
Button btnnext3;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_pink, container, false);
        btnnext3=(Button) view.findViewById(R.id.btnnext3);
        btnnext3.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {


            }
        });
        return view;
    }
}

The adapter for the viewpager:

package pl.looksok.viewpagerdemo;

import java.util.ArrayList;
import java.util.List;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class MyPagerAdapter extends FragmentPagerAdapter {

    private List<Fragment> fragments;

    public MyPagerAdapter(FragmentManager fm) {
        super(fm);
        this.fragments = new ArrayList<Fragment>();
        fragments.add(new FragmentBlue());
        fragments.add(new FragmentGreen());
        fragments.add(new FragmentPink());
    }

    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }
}

The custom class for the viewpager:

package pl.looksok.viewpagerdemo;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

public class CustomViewPager extends ViewPager {

    private boolean isPagingEnabled = false;

    public CustomViewPager(Context context) {
        super(context);
    }

    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return this.isPagingEnabled && super.onTouchEvent(event);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        return this.isPagingEnabled && super.onInterceptTouchEvent(event);
    }

    public void setPagingEnabled(boolean b) {
        this.isPagingEnabled = b;
    }
}

The code which i am using now shows the following error on clicking the first button:

09-20 10:54:18.632: E/AndroidRuntime(9401): FATAL EXCEPTION: main
09-20 10:54:18.632: E/AndroidRuntime(9401): android.content.ActivityNotFoundException: Unable to find explicit activity class {pl.looksok.viewpagerdemo/pl.looksok.viewpagerdemo.FragmentGreen}; have you declared this activity in your AndroidManifest.xml?
09-20 10:54:18.632: E/AndroidRuntime(9401):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405)
09-20 10:54:18.632: E/AndroidRuntime(9401):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
09-20 10:54:18.632: E/AndroidRuntime(9401):     at android.app.Activity.startActivityForResult(Activity.java:2827)
09-20 10:54:18.632: E/AndroidRuntime(9401):     at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:824)
09-20 10:54:18.632: E/AndroidRuntime(9401):     at android.app.Activity.startActivity(Activity.java:2933)
09-20 10:54:18.632: E/AndroidRuntime(9401):     at pl.looksok.viewpagerdemo.FragmentBlue$1.onClick(FragmentBlue.java:25)
09-20 10:54:18.632: E/AndroidRuntime(9401):     at android.view.View.performClick(View.java:2538)
09-20 10:54:18.632: E/AndroidRuntime(9401):     at android.view.View$PerformClick.run(View.java:9152)
09-20 10:54:18.632: E/AndroidRuntime(9401):     at android.os.Handler.handleCallback(Handler.java:587)
09-20 10:54:18.632: E/AndroidRuntime(9401):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-20 10:54:18.632: E/AndroidRuntime(9401):     at android.os.Looper.loop(Looper.java:130)
09-20 10:54:18.632: E/AndroidRuntime(9401):     at android.app.ActivityThread.main(ActivityThread.java:3689)
09-20 10:54:18.632: E/AndroidRuntime(9401):     at java.lang.reflect.Method.invokeNative(Native Method)
09-20 10:54:18.632: E/AndroidRuntime(9401):     at java.lang.reflect.Method.invoke(Method.java:507)
09-20 10:54:18.632: E/AndroidRuntime(9401):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
09-20 10:54:18.632: E/AndroidRuntime(9401):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
09-20 10:54:18.632: E/AndroidRuntime(9401):     at dalvik.system.NativeStart.main(Native Method)

So the question is how to use intent in between fragments.Is there a better way to divide the sign up form into three parts without using viewpager?

like image 429
user3852672 Avatar asked Sep 20 '14 05:09

user3852672


1 Answers

You can't call your Fragment like this,

  Intent intent = new Intent(view.getContext(), FragmentGreen.class);
  view.getContext().startActivity(intent);
  getActivity().finish();

you need to call your fragment like this way

    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    FragmentGreen llf = new FragmentGreen();
    ft.replace(R.id.listFragment, llf);
    ft.commit();

Intent is basically used for call one activity from another. For add new Fragment you can't use Intent. For that you have to use FragmentManager and for open fragment FragmentTransaction.

for more details go Here

like image 194
Piyush Avatar answered Dec 29 '22 00:12

Piyush