Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if intent is calling or Activity is started by default

how can I check if Activity is started by default or a method of the Activity is called from an intent in an other activity? I think at the moment my Code is very bad, because i handle it over a Try/Catch It works fine, but i want better code

public class MyScan extends Activity {
public final static String EXTRA_MESSAGE = ".MESSAGE";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    checkIntent();
}

public void checkIntent() {
    try {
        Intent i = getIntent();
        String method_name = i.getStringExtra("method_name");// is firing an error if there is no intent call
        if (method_name.equals("scanBarcode")) {
            scanBarcode2();// That starts my method
        }
    } catch (Exception e) {
        setContentView(R.layout.activity_my_scan); // that shows just my Content
    }

}
....

Thanky you for your hint Alex Terreaux

i changed the code this way

public void checkIntent() {
    Intent i = getIntent();
    if (i != null) {
        String method_name = i.getStringExtra("method_name");
        if (method_name != null && method_name.equals("scanBarcode")) {
            scanBarcode2();
        } else {
            setContentView(R.layout.activity_my_scan);
        }
    }
}

and that works.

like image 362
Alex S. Avatar asked Jan 09 '23 21:01

Alex S.


1 Answers

Try checking if the result of getIntent() is null.

like image 62
Alex Terreaux Avatar answered Jan 30 '23 22:01

Alex Terreaux