0

Only on 2nd click the button loads the new activity.

Usually this problem happens often when I define an onCLick() method in the .xml and also have it in the activity. This is here not the case.

        <Button
            android:id="@+id/settingsBtn"
            android:layout_width="192dp"
            android:layout_height="60dp"
            android:layout_above="@+id/exitBtn"
            android:layout_alignParentStart="true"
            android:layout_alignParentEnd="true"
            android:layout_gravity="end|bottom"
            android:layout_marginStart="-2dp"
            android:layout_marginEnd="2dp"
            android:layout_marginBottom="24dp"
            android:background="@android:color/black"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:text="@string/settings"
            android:textColor="@android:color/white"
            android:textSize="24sp" />
private Button mSettingsButton;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
               .
               .
               .
        connectUiElements();
        setUpUiListeners();
}
.
.
.
private void connectUiElements() {
    mSettingsButton = (Button) findViewById(R.id.settingsBtn);
}

private void setUpUiListeners() {
            mSettingsButton.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(getApplicationContext(), 
                                                   SettingsActivity.class);

                        startActivity(intent);
                    }
                }
        );
}

Usually I would expect, that the buttons works when I click once.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
NobodyIsPerfect
  • 182
  • 1
  • 12
  • all looks fine but why you have added android:focusable="true" android:focusableInTouchMode="true" ? without this also button should be able to take click events – Ashok Kumar Sep 13 '19 at 15:22
  • legacy code, removed that ...thanks :P... ill go check it with a physical device again... maybe it behaves only in the emulator so strange – NobodyIsPerfect Sep 13 '19 at 15:30
  • Btw you shouldn't use application context for intent. See [here](https://stackoverflow.com/a/35189719/1512199) to learn more about when to use which context. – Fatih Santalu Sep 13 '19 at 15:38
  • also thanks, I was not aware of this... as this is the way shown in every beginner tutorial ^^. doesn't solve my problem though :(. I would post the whole code but its far more then 2.000 lines of code with every call other classes etc. Is it possible that the threat is too busy? that he cannot take that first click? – NobodyIsPerfect Sep 13 '19 at 15:43
  • replace the getApplicationContext with NameOfYourCurrentActivity.this. Also, I'm not really sure why you bind the views and set the listeners in separate methods in the first place. Why not do it straight away inside your onCreate? I'm not saying these changes will fix your problem, it's just a suggestion to use more common practices. – Nikos Hidalgo Sep 13 '19 at 16:19
  • There's nothing wrong with your code here. But you should check if you set click listener to this button more than once. Also check out [this](https://stackoverflow.com/a/17459413/1512199) might be same for you – Fatih Santalu Sep 13 '19 at 16:21
  • @NikosHidalgo I did it because thats the way programming was taught to me, outsourcing big functions in more smaller functions for readability etc. And yes I replaced that and made code adjustments but i used `getBaseContext()`which also offers activity context according to the link and table fomr @santalu. I'll post a solution here if i find any. It seem's like that the button can't be the problem. – NobodyIsPerfect Sep 13 '19 at 16:31
  • That's a nice practice to follow, not necessarily with binding your views and setting the listeners though. You're meant to organise big chunks of code in smaller methods that are reusable. Binding a view is not reusable the way you've hardcoded it to your specific views. If you want to minimise your code you could use android annotations to avoid the boilerplate that is the view binding and listeners setting. – Nikos Hidalgo Sep 16 '19 at 08:29
  • @NikosHidalgo thanks for explaining. should I delete this question now? As this is not a fault of the button. Haven't figured out yet whats causing this bug. Maybe I'll find it today. – NobodyIsPerfect Sep 16 '19 at 09:17
  • It's not a bad idea to leave the question here while you still have the problem. Someone could come up with a solution or suggest a workaround that they figured out when in similar situation. – Nikos Hidalgo Sep 16 '19 at 09:28

0 Answers0