-1
@Override
protected void onPostExecute(String result) {
    alertDialog.setMessage(result);
    alertDialog.show();

    if(result.contains("login success")){

        Intent intent = new Intent(context, Viewlpost.class);
        context.startActivity(intent);         
    }
}

This is my code, to redirect the user from loginactiivty to a fragment activity

This syntax won't work because the application crashes after doing the intent.

I also need a button inside a fragment that needs to redirect to another fragment when clicked. Account to edit account. This is the code

changepword.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on click
            Intent intent = new Intent(getActivity(),EditAccount.class);
            startActivity(intent);
        }
    });
wannabes
  • 25
  • 6
  • Do you want to send data from activity to fragment? You will have to use data in bundle in thay case. Intent is only used to send data between two activities. – Kinjal Rathod Sep 30 '18 at 04:11

3 Answers3

2

To pass data from one activity to another activity you must use following :

Intent intent = new Intent(Activity1.this,Activity2.class);
intent.putExtra("key",data);
startActivity(intent);

To pass to a fragment use following lines of code :

Bundle bundle = new Bundle();
bundle.putExtra("key",data); 

 FragmentManager fragMan = getFragmentManager();
 FragmentTransaction fragTransaction = fragMan.beginTransaction(); 
 YourFragment fragment = new YoutFragment();
 fragment.setArguments(bundle);
 fragTransaction.add(R.id.fragmentLayout, fragment);
 fragTransaction.commit();
Kinjal Rathod
  • 499
  • 4
  • 12
1

Fragments aren't invoked like Activities through an Intent. They can only exist as part off an Activity, that is what they are designed for

FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(YourFragment.newInstance(), null);
ft.commit();
1

From a fragment to an activity

                Intent intent = new Intent(getActivity(),ViewLPost.class);
                startActivity(intent);

From an activity to a fragment it's supposed you want to go back , so finish(); is sufficient to do the task

Ahmad.ak
  • 93
  • 6