-1

I am working on stripe-terminal-android app, they have built-in app, but I wanted to customise it according to use-case.

link of stripe-app-repo

when I open app it first gives me fragment, to Discover Readers, link-of-code,

but I wanted that to automatically clicked and without any click it should work as it was clicked, so that no need to manually Discover-Readers, everytime.

@Override
public void onViewCreated(@NotNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    view.findViewById(R.id.discover_button).setOnClickListener(v -> {
    final FragmentActivity activity = getActivity();
    if (activity instanceof NavigationListener) {
        ((NavigationListener) activity).onRequestDiscovery(viewModel.getSimulated());
    }
});

but, current code is to call onRequestDiscovery, is executed only after clicking-button, how can I do like we do in javascript?

document.getElementById("demo").click()) or similar-one? so no need to manually-click and it would proceed further.

Edit after answer by Sebastein.

added this line, but I wanted to click and call function onRequestDiscovery() which is not working.

enter image description here view.findViewById(R.id.discover_button).performClick()

screenshot added after edited-response by Sebastein,

enter image description here

Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
GD- Ganesh Deshmukh
  • 1,456
  • 3
  • 24
  • 36
  • found this link useful, but working/figuring it out, how to make it work. https://stackoverflow.com/questions/12637693/android-calling-activity-from-fragment – GD- Ganesh Deshmukh Nov 08 '19 at 13:27
  • so so sorry, to post-without reading error, I by-mistakenly wrote function `getSharedPreferencesgetSharedPreferences` twice, while I must have tried `getSharedPreferences()` once only. sorry community for that, but I won't delete this post to hide my mistake. – GD- Ganesh Deshmukh Nov 08 '19 at 14:16

1 Answers1

1

use method performClick() after adding the click listener :

@Override
public void onViewCreated(@NotNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    Button button = view.findViewById(R.id.discover_button)
    button.setOnClickListener(v -> {
        final FragmentActivity activity = getActivity();
        if (activity instanceof NavigationListener) {
            ((NavigationListener) activity).onRequestDiscovery(viewModel.getSimulated());
        }
    button.performClick();
});

but for me the best way is not to start the activity in the fragment but directly in the activity which instantiate the fragment.

SebastienRieu
  • 1,443
  • 2
  • 10
  • 20
  • thanks @Sebastien but it's now working, I am updating my answer to your reply. – GD- Ganesh Deshmukh Nov 08 '19 at 13:34
  • i've said you to call performClick() AFTER setOnClickListener ! – SebastienRieu Nov 08 '19 at 13:38
  • okay Sebastein, I tried that to adding after `setOnClickListener` `view.findViewById(R.id.discover_button).setOnClickListener(v -> { final FragmentActivity activity = getActivity(); if (activity instanceof NavigationListener) { ((NavigationListener) activity).onRequestDiscovery(viewModel.getSimulated()); } }); view.findViewById(R.id.discover_button).performClick(); ` – GD- Ganesh Deshmukh Nov 08 '19 at 13:41
  • could you give whole snippet-reference please? – GD- Ganesh Deshmukh Nov 08 '19 at 13:42
  • Hi Sebastein I already saw your newly-edited reply but it's too didn't work. I am updating my question and posting screenshot of error-while-compiling. `getSharedPreferencesgetSharedPreferences` – GD- Ganesh Deshmukh Nov 08 '19 at 13:52
  • thre's another error in your fragment code, find where are you using getSharedPreferencesgetSharedPreferences, the error comes from that – SebastienRieu Nov 08 '19 at 13:56
  • new error is thrown at getSharedPreferences, https://github.com/stripe/stripe-terminal-android/blob/dcb8f4af62b75b51b00f6e30e2bae40e5852410a/Example/javaapp/src/main/java/com/stripe/example/javaapp/fragment/TerminalFragment.java#L44 – GD- Ganesh Deshmukh Nov 08 '19 at 14:00
  • 1
    Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/202056/discussion-between-sebastienrieu-and-ganeshdeshmukh). – SebastienRieu Nov 08 '19 at 14:04
  • hey @Sebastein, I have replied in chat. – GD- Ganesh Deshmukh Nov 08 '19 at 14:08
  • it was my error after your first reply, sorry for pastingErrorWithoutReadingError. – GD- Ganesh Deshmukh Nov 08 '19 at 14:17