0

how is this possible using fragment

list item on click ---→ fragment on click--- → another fragment

i am using this code problems is crashed app ..

public class HomeFragment extends Fragment implements OnClickListener {
private static final String TAG_FRAGMENT = null;
RelativeLayout Homeremadies;
RelativeLayout HealthyLiving;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_home);
    Homeremadies=(RelativeLayout)findViewById(R.id.rlHome);
    HealthyLiving=(RelativeLayout)findViewById(R.id.Healthyliving);

    Homeremadies.setOnClickListener(this);
    HealthyLiving.setOnClickListener(this);
}

public void onClick(View v) {
    // TODO Auto-generated method stub
    if (v.getId()==R.id.rlHome)
    {

        startActivity(new Intent(v.getContext(),cat_homeremadies.class)); 
    }
    else if (v.getId()==R.id.Healthyliving)
    {
    Toast.makeText(this, "Facebook pressed!", Toast.LENGTH_SHORT).show();   
    }
}

} 
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • Post your LogCat so we can see the errors – Cruceo Apr 30 '14 at 14:21
  • This one has to work: [How to start a new Fragment from fragment?](http://stackoverflow.com/questions/13216916/how-to-start-a-new-fragment-from-fragment#answer-13221546) – Yehor Nemov Apr 30 '14 at 14:23

1 Answers1

0

You are not using fragment correctly, please have a look on the facebook-android-sdk sample code, it has very good demonstration of fragment transition.

Below is an example to implement fragment in android app with correct implementation of fragment transition.

MainActivity.java

public class MainActivity extends FragmentActivity {

    private static final String USER_SKIPPED_LOGIN_KEY = "user_skipped_login";

private static final int SPLASH = 0;
private static final int SELECTION = 1;
private static final int SETTINGS = 2;
private static final int FRAGMENT_COUNT = SETTINGS +1;

private Fragment[] fragments = new Fragment[FRAGMENT_COUNT];

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
}

    FragmentManager fm = getSupportFragmentManager();
    SplashFragment splashFragment = (SplashFragment) fm.findFragmentById(R.id.splashFragment);
    fragments[SPLASH] = splashFragment;
    fragments[SELECTION] = fm.findFragmentById(R.id.selectionFragment);
    fragments[SETTINGS] = fm.findFragmentById(R.id.userSettingsFragment);

    FragmentTransaction transaction = fm.beginTransaction();
    for(int i = 0; i < fragments.length; i++) {
        transaction.hide(fragments[i]);
    }
    transaction.commit();
}

public void showSettingsFragment() {
    showFragment(SETTINGS, true);
}

public void showSplashFragment() {
    showFragment(SPLASH, true);
}

   splashFragment.setSkipLoginCallback(new SplashFragment.SkipLoginCallback() {
        @Override
        public void onSkipLoginPressed() {
            userSkippedLogin = true;
            showFragment(SELECTION, false);
        }
    });


private void showFragment(int fragmentIndex, boolean addToBackStack) {
    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction transaction = fm.beginTransaction();
    for (int i = 0; i < fragments.length; i++) {
        if (i == fragmentIndex) {
            transaction.show(fragments[i]);
        } else {
            transaction.hide(fragments[i]);
        }
    }
    if (addToBackStack) {
        transaction.addToBackStack(null);
    }
    transaction.commit();
    }
}

SplashFragment.java

public class SplashFragment extends Fragment {

private LoginButton loginButton;
private TextView skipLoginButton;
private SkipLoginCallback skipLoginCallback;
private CallbackManager callbackManager;

public interface SkipLoginCallback {
    void onSkipLoginPressed();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.splash, container, false);

    callbackManager = CallbackManager.Factory.create();
    loginButton = (LoginButton) view.findViewById(R.id.login_button);
    loginButton.setReadPermissions("user_friends");
    loginButton.setFragment(this);
    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            Toast.makeText(getActivity(), "Login successful", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel() {
            Toast.makeText(getActivity(), "Login canceled", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(FacebookException exception) {
            Toast.makeText(getActivity(), "Login error", Toast.LENGTH_SHORT).show();
        }
    });

    skipLoginButton = (TextView) view.findViewById(R.id.skip_login_button);
    skipLoginButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (skipLoginCallback != null) {
                skipLoginCallback.onSkipLoginPressed();
            }
        }
    });

    return view;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, resultCode, data);
}

public void setSkipLoginCallback(SkipLoginCallback callback) {
    skipLoginCallback = callback;
}

These sample uses static fragment loading, instead of dynamic fragment loading, so the details may vary a bit with dynamic fragment implementation, but you can get the idea from this.

hkCitizen
  • 27
  • 6