Which is the correct way to start an Activity? - android

Let's say the Activity I want to start is named "OccupyThePieShop"
I was previously using this methodology to start an Activity:
Intent oTPS = new Intent();
timeIntervalConfigIntent.setClassName("com.aXX3AndSpace.KeepInTouch",
"com.aXX3AndSpace.KeepInTouch.OccupyThePieShop");
startActivity(oTPS);
...but was told that this is more the norm:
Intent oTPS = new
Intent(KeepInTouchActivity.this, OccupyThePieShop.class);
KeepInTouchActivity.this.startActivity(oTPS);
...and so I replaced my calls to startActivity() with that usage.
Now, I've come across a couple more ways which seem quite "elegant," namely:
startActivity(new Intent(getApplicationContext(), OccupyThePieShop.class));
...and:
Intent intent = new Intent(this, OccupyThePieShop.class);
startActivity(intent);
Is one way preferred over the others, and if so, why?

I think this is probably an issue of personal preference. I like startActivity(new Intent(this, OccupyThePieShop.class)); because, as you said, it is elegant.

Related

Android intents are launched in reverse order

I am working on an android app where I from one activity create and start a number of activities using intents (and close these using the finish() method()) however when I run the app the order the activities are launched in is the reversed of the creation order. I realize this is because the intents are not launched imminently on the startActivity(Intent) call, but added to a queue, but I would really like to preserve the order. My question is, is there any way to keep the order of the activities as they are created? See pseudo code for better explanation
**Main Class**
Intent intent1 = new Intent(this, activity1);
startActivity(intent1);
Intent intent2 = new Intent(this, activity2);
startActivity(intent2);
Intent intent3 = new Intent(this, activity3);
startActivity(intent3);
**Intent class**
....
finish()
However when I launch the app the activity 3 is launched first, followed by 2 and finally 1 ---> how do I preserve the order so they launch 1->2->3?
Thanks a lot in advance
You can start first activity, in it's onCreate(...) method start second and then in onCreate(...) of second activity start third. So you will get 1->2->3 and 3 will be on the top of the stack

Sending intent to activity from BroadcastReceiver

I have yet another question. I did some research into how to properly send an intent from a BroadcastReceiver to an activity. everyone suggests doing the following:
To construct an intent and use the context provided in the receiver to start that intent. However, I would always get an error when trying to do so saying
AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
So when I googled about that error, people seemed to suggest that you want to add specific flags to the intent. But even with this flags present I am getting the same error. Any help would be appreciatd.
Intent intentMain = new Intent(context.getApplicationContext(), MainActivity.class);
intentMain.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
intentMain.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("com.hennessylabs.xxx.FROM", senderNum);
intent.putExtra("com.hennessylabs.xxx.MSG", message);
context.startActivity(intentMain);
It looks like you are trying to start MainActivity.class from your BroadcastReceiver. That is fine. Though I don't see why you have done what you did in the 2nd and 3rd lines you provided when adding flags. Why don't you just do this:
intentMain.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
So your whole code block should look like this:
Intent intentMain = new Intent(context.getApplicationContext(), MainActivity.class);
intentMain.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("com.hennessylabs.xxx.FROM", senderNum);
intent.putExtra("com.hennessylabs.xxx.MSG", message);
context.startActivity(intentMain);
Intent intent = new Intent(context,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
and then in manifest file
in mainactivity
launchmode= singleTask
or as per your requirements, you can use some other combinations also

startActivity() doesn't work when app is in background (In this special case)

I'm trying to bring my app from background to foreground. In onHandleIntent() of my custom IntentService class, I have:
Intent intent = new Intent();
intent.setClass(getApplicationContext(), MainActivity.class); // Also tried with "this" instead of getApplicationContext()
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(intent);
Now this code works at first sight but I found a scenario where it doesn't work. If you have the app opened and you put it to background via home button and execute startActivity() within ~5 second, there will be a delay before your app will come to foreground. This is a known implementation and you can find the topic discussed on stackoverflow. In this scenario, the app succeeded in coming from background to foreground.
If you repeat the same experiment above, but instead of waiting for the app to come to foreground, go browse (scroll, swipe, etc) around your phone (I was browsing around the google playstore). The result is that startActivity() will get called but the app will not come to the foreground.
I'm not asking for a solution but more of an explanation on why this is happening. Is this intended behavior?
Use the context of your class.
For instance :
Intent intent= new Intent(context, other.class)
Instead of getapplicationContext()
Use the code :
private void startMenuActivity() {
Intent i = new Intent(this, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
finish();
}
the below code works for me,
val login = Intent(applicationContext, SignInActivity::class.java)
login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
login.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
applicationContext.startActivity(login)

Start Activity with a variable

How can I do the name of the activity variable with a string?
Intent in = new Intent(MainActivity.this,
xxx.class);
If my string is "a" it will be:
Intent in = new Intent(MainActivity.this,
a.class);
else, if my string is "b":
Intent in = new Intent(MainActivity.this,
b.class);
how can I concatenate this? Thank you so much
how can I concatenate this?
Ideally, you don't. You use something else, like a switch statement or a HashMap<String,Class> lookup or something.
That being said, you are welcome to use Class.forName() if you really want.
I cannot check it right now but this should work:
Intent I = new Intent(this, Class.forName("your.package.ClassName"));
However this is a little dangerous IMHO. This would allow to start any activities from outside of your app. This would e.g. allow to open some internal activities from outside which are not exported.

Is it possible to free up memory by finishing an activity after calling another?

Hopefully the title wasn't to confusing but what I meant was the following:
Lets say activity A starts activity B by calling:
Intent myIntent = new Intent(Activity_A.this, Activity_B.class);
Activity_A.this.startActivity(myIntent);
Could I save/free up some memory by finishing Activity_A after Activity_B is begun (if thats even possible). Maybe through the following:
Intent myIntent = new Intent(Activity_A.this, Activity_B.class);
Activity_A.this.startActivity(myIntent);
Activity_A.finish();
Or would Acitivty_A call startActivity() and wait for Activity_B to finish before it called finish()?
The idea would then be that when the users end with Activity_B, it would just restart Acitivity_A (and finish itself in a similar fashion)? Would this create too much overhead? Thanks for any answers and I apologize if the formatting of this post isn't correct.
What you are trying to do is platform's job. You shouldn't really care about optimizations like this.
Still, if you do, this will work just fine:
Intent intent = new Intent(this, OtherActivity.class);
startActivity(intent);
finish(); // finishes this
This will not necessarily save any memory, and I'm not immediately sure of any way to do this. When you go from one activity to another, the first activity is closed and its state bundled. It's not really open anymore at that point.

Categories

Resources