How to call a method unless it was opened as an activity?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to call a method unless it was opened as an activity?



Is it possible to trigger a method when an Activity is opened by an intent.
I found onNewIntent() on articles but I have no idea how that works.


onNewIntent()



For example if I had the method loadData(). In the loadData method I will receive an Intent and get an array extra.


loadData()


loadData


public void loadData()
Bundle bundle = getIntent().getExtras();
contactsList = (ArrayList<Contact>)bundle.getSerializable("array");



But i only want loadData() to be called unless it is opened by an intent or it will throw a bug.



This article How to call a method using intent was similar to what I wanted because it seems like the getIntent is called in the oncreate. I don't know if that will work or not.


getIntent


oncreate



Any help would be appreciated, thanks.




2 Answers
2



Check intent have values or null .if its have the value call your method


if(getIntent().getExtras() != null)

loadData()



You can send a parameter to the Activity and check if that parameter exists to trigger the method. Does this fit what you want?


Intent intent = new Intent(this, OtherActivity.class);
intent.putExtra("loadData", true);



In the new activity, check that value:


Boolean loadData = getIntent().getExtras().getBoolean("loadData");
if(loadData) ...






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard