I need help with Android and Java

I'm struggling to figure out how to handle the situation on Android, where the code continues to run when I really don't want it. In the onCreate () activity, I have something that I need to do in order, and I cannot get the code to continue to execute until some things happen. I understand that this is how Android and Java behave, and I'm struggling to find another way to achieve what I need.

ShowEula is a class that I created to show a simple dialog with my EULA. I really need to wait until the user agrees or agrees with EULA before calling DBGetOnlineVersionNumber (). Maybe I just won’t go that right. Anyway, here is a piece of code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);        
    setContentView(R.layout.main);
    String version;
    ShowEula();     
    version = DBGetOnlineVersionNumber();
    ....
+3
source share
4 answers

You need to rethink things in terms of the event driven model used by android.

You cannot wait for user interface events in onCreate because no user interface events can be delivered until the onCreate command returns.

Perhaps you could organize things like a software state machine.

onCreate will show EULA and set state to EULA_SHOWING

accept will push state to EULA_ACCEPTED and display something like this

Of course, you probably do not want to show EULA every time you start, but only after installation, but you can handle it with something stored in the settings.

+4
source

, , . , ...

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);        
    setContentView(R.layout.main);
    String version;

    if (Settings.isEulaBeenShown() == false) {
        /* showEula would call stuffToDoIfUserAgreesToEULA on a positive response was given */
        ShowEula();
    }
    else if (Settings.isEULAAgreed() == true) {   
        stuffToDoIfUserAgreesToEULA()
    }
    ....
 }

 private void stuffToDoIfUserAgreesToEULA() {
     String version = DBGetOnlineVersionNumber();
 }
+3

, 2 xml .

EULA Accept/Deny, .

, EULA, .

, , ContentView .

onCreate(), , .

private static boolean EulaRequired = true;
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);        
    EulaRequired = sharedprefs.getBoolean("firsttime", true); 
    inflateLayout();    
 }

 private void inflateLayout() {
   if (EulaRequired) {
     this.setContentView(R.layout.eula);
   } else {
     this.setContentView(R.layout.application);
     DBGetOnlineVersionNumber();
   }
 }
 private void stuffToDoIfUserAgreesToEULA() {
     String version = DBGetOnlineVersionNumber();
 }
 void AcceptClicked(Event e) {
   EulaRequired = false;
   inflateLayout();
 }

- , , .

0

Download the Eula.java file and implement the OnEulaAgreedTo interface. This site provides a complete example and instructions: http://android-code-crumbs.blogspot.com/

0
source

Source: https://habr.com/ru/post/1778646/


All Articles