Search code examples
androiduser-interfaceandroid-intentoncreate

Using getIntent() in onCreate() prevents ui from loading... why?


I have an activity that list authorized users, and I'm using getIntent() in my activity's onCreate() method to check if the activity should display a pre-filled add user dialog when it loads. Here is my code:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sms_auth_manager);
    
    try{ //check if the activity was launched with a prefill intent
        Intent intent = getIntent();
        int id = (intent.getIntExtra("notification_id",0));
        NotificationUtils notificationUtils = new NotificationUtils();
        notificationUtils.hideNotification(this,id);
        if (intent.getBooleanExtra("isPrefill",false)){
            String preFill = intent.getStringExtra("sender");
            showAddUserDialog(preFill);
        }
    }catch (Exception ignore){} //exception swallowed means no dialog
    
    refreshList(); //loads the list of users into the main listview of the activity

}

My problem is that the call to refreshList() is not resulting in the list being refreshed. I tried putting it before the try block as well, and either way it won't work. I tested with the try block commented out though, and that does work, but then I lose the functionality.

Here is the code of the refreshList() method, in case it's necessary:

private void refreshList(){
    SharedPreferences sharedPrefs = getSharedPreferences("users",0);

    LinearLayout ll = findViewById(R.id.ll);
    
    ll.removeAllViews();
    
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT
    );
    
    
    for (String sender : sharedPrefs.getAll().keySet()) {
        CheckBox checkBox = new CheckBox(this);
        checkBox.setText(getContactDisplayName(sender));
    
        checkBox.setTag(sender);
        
        try{
            checkBox.setChecked(sharedPrefs.getBoolean(sender,false));
        }catch (Exception e){
            checkBox.setChecked(false);
        }
        
        checkBox.setLayoutParams(layoutParams);
        checkBox.setOnClickListener(v -> {
            try {
                sharedPrefs.edit().putBoolean(sender, checkBox.isChecked()).apply();
            }catch (Exception e){
                Toast.makeText(this, "Preference not updated.\n"+e.getMessage(),
                    Toast.LENGTH_SHORT).show();
            }
        });
        checkBox.setOnLongClickListener(v -> {
            showPopupMenu(v);
            return false;
        });
        ll.addView(checkBox);
    }
}

Why does the try block prevent the UI from refreshing, and how can I acheive the functionality I am looking for?


Solution

  • If you intend to run the refreshList method regardless if there is an exception or not you can try using the finally block which would run the code regardless of the exception.