0

When I am in a fragmentB, how to simulate a click on a button located in an activity in the stack.

LoginActivityA => fragmentB.

I want to simulate something like this:

LoginActivityA.btnClick() from the fragmentB

I tried to use this tickets with no success: Ticket1, Ticket2, Ticket3
Here is my code:
FragmentB:

//BEGIN TEST
String message="hello ";
Intent intent = new Intent(getActivity(), LoginActivity.class);
intent.putExtra("MESSAGE",message);
getActivity().setResult(2,intent);
getActivity().startActivityForResult(intent,2);
getActivity().finish();//finishing activity
//END TEST

LoginActivityA:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        // check if the request code is same as what is passed  here it is 2
        if(requestCode==2)
        {
            //do the things u wanted
        }
    }  

I precise that I try not to get multiple LoginActivity and Fragment launched. I would like to get only one stack with LoginActivity => Fragment

Any ideas?

EDIT:

I tried this from this tichet with no success:

((LoginActivity)getActivity()).login();

I launched the activity function from the presented fragment with an error

"No acceptable module found. Local version is 0 and remote version is 0."

ΩlostA
  • 2,501
  • 5
  • 27
  • 63

2 Answers2

0

You can use an interface with an onClick method that is implemented by your activity and then in fragment B call the method onClick whenever you want and pass the activity to it.

Although I presume what you are doing is wrong and you should change your mind around it.

Elmira Frhn
  • 320
  • 4
  • 14
0

This is not a good practice, but if you want you can call your activity UI elements by

((Button) getActivity().findViewById(R.id.button_id)).performClick();

Always remember, Android is a well architect platform, if you are trying to do something which is difficult, probably you should not do it. Rethink about your your design patterns.

Samuel Robert
  • 10,106
  • 7
  • 39
  • 60