intent flag over intent startActivity(i); - java

#Override
public void onClick(View view) {
// Launching News Feed Screen
Intent i = new Intent(getApplicationContext(), Profile.class);
startActivity(i);
}
});
what is the difference of using this code and what difference does it do on the program compared to this doe
Intent i = new Intent(CurrentActivityName.this, NextActivityName.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);

First one uses getApplicationContext() to launch the intent.
Application context is attached to the application's life-cycle and will always be same throughout the life of application. So if you are using Toast, you can use application context or even activity context (both) because a toast can be raised from anywhere with in your application and is not attached to a window.
Second one uses the Activity context.
Activity context is attached to the Activity's life-cycle and can be destroyed if the activity's onDestroy is raised. If you want to launch a new activity, you must need to use activity's context in its Intent so that the new launching activity is connected to the current activity (in terms of activity stack).
So, Whenever you need to manipulate Views then go for Activity-Context, else Application-Context would be enough.
Source: this answer at difference and when to use getApplication(), getApplicationContext(), getBaseContext() and someClass.this question.
Also, reading some of above links would help:
What's the difference between the various methods to get a Context?
getApplicationContext(), getBaseContext(), getApplication(), getParent()

Related

Does onCreate create an instance of the class? [duplicate]

For example
Intent intent = new Intent(this, SecondActivity.class);
eclipse error: The method setClass(Context, Class) in the type
Intent is not applicable for the arguments (FirstActivity.ClickEvent,
Class)
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
But that will be correct.
Anybody can explain the difference between those two ?
Thanks.
this refers to your current object. In your case you must have implemented the intent in an inner class ClickEvent, and thats what it points to.
Activity.this points to the instance of the Activity you are currently in.
Shubhayu's answer is correct, but I just want to make clear for anyone who see this question that this and Activity.this is the same if you are using it directly in the activity.
This is answered here
Example:
#Override
protected void onResume() {
super.onResume();
Log.d("Test", this.toString());
Log.d("Test", MainActivity.this.toString());
}
Result:
D/Test: com.example.app.MainActivity#e923587
D/Test: com.example.app.MainActivity#e923587
When you are pointing to this inside click event, it is pointing to the click listener.
You are intent to transfer control from one activity to another and for that u ll have to specify an event basically and hence the error.
this means the entire activity and firstactivity.this means an event occurring for example a a button clicked.........

Android Is An Intent Unique To An Activity or Global

I'm curious as to whether an intent is unique to a set activity or not.
For example, my current usage of an intent is to give it some data from Activity 1 and pass it on to Activity 2.
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("id", id.getText().toString());
intent.putExtra("weight", weight.getText().toString());
intent.putExtra("zipTo", zipTo.getText().toString());
intent.putExtra("locationId", locationID.getSelectedItem().toString());
intent.putExtra("imageNum", 1);
startActivity(intent);
Now, in MainActivity I can use the following code to retrieve the data passed from the previous activity.
Bundle bundle = getIntent().getExtras();
id = bundle.getString("id");
weight = bundle.getString("weight");
locationID = bundle.getString("locationID");
zipTo = bundle.getString("zipTo");
After doing some work in MainActivity, I need to return to the previous activity that sent us to MainActivity (and I need the data that was originally sent to return).
Do I need to redefine an intent, and do "intent.putExtra" for each value again in MainActivity before sending it to the first Activity? Or is the intent global, and once defined in one activity I can use it in the others with getIntent().getExtras()?
Intents are not global. You only have access to the bundle in the activity you started with the intent passed.
You should use startActivityForResult(intent). This provides the behavior youre looking for.
Getting a result from an activity
The idea is you start the new intent. In this case 'MainActivity'. Once you are done with your logic in MainActivity you use the strategy above to pass a result back to the activity that started MainActivity.
information passed through intents are not global from one activity to another, what is global is shared preferences and information that is metadata which is stored in manifest.xml.

Android Transition between Activities from non-Activity Class

I'm using an activity context from a non-activity class.
I don't want any transition between Activities, but sometimes there is no transition and sometimes there is.
Here is my code:
Intent myIntent = new Intent(this.context, Lose.class);
context.startActivity(myIntent);
((Activity)context).overridePendingTransition(0, 0);
Does anyone understands what part of my code causes this inconsistent behaviour?
Try adding this to your Intent:
myIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);

How to destroy an activity

In fact, I am new to Android App Development. In my application, I have a couple of activities and I have provided my users with an exit option menu to be able to leave the application. But there is a problem. When they hit the Exit button, they are able to leave the application but when they enter the application for the second time, the page that they left off the last time will be launched.
Here comes my code:
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case 0 :
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
Toast.makeText(this, "Goodbye Dear", Toast.LENGTH_LONG).show();
break;
Android Activity has two methods onPause and onDestroy where you can do the necessary cleanup.
http://developer.android.com/reference/android/app/Activity.html
Instead of using finish(), use System.exit(0);.
You have to override onPause and/or onDestroy methods inside your activity and delete your view within these methods.
The problem in your code is that Intent.FLAG_ACTIVITY_NEW_TASK doesn't remove your current Task. Read more about it here: Task and Back Stack | Android Developers.
Try using Intent.FLAG_ACTIVITY_CLEAR_TOP. From the documentation we can see that this gives the desired behavior.
If set, and the activity being launched is already running in the
current task, then instead of launching a new instance of that
activity, all of the other activities on top of it will be closed and
this Intent will be delivered to the (now on top) old activity as a
new Intent.

Android. How to start activity without creating new one?

I tried to set attribute android:launchMode="singleTask" for calling Activity, but it still does not works as I expected.
I need that method onCreate(Bundle bundle) to be called only once, but it still calls each time when I start Activity.
I start Activity using code like this:
public void onClick(View v) {
Intent myIntent = new Intent(v.getContext(), NextActivity.class);
startActivity(myIntent);
}
Please let me know what I am doing wrong
It must be like this:
android:launchMode="singleTop"
and calling:
Intent intent= new Intent(context, YourActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Already existed topic about this: Android: new Intent() starts new instance with android:launchMode="singleTop"
Even if you make your launch mode 'singleTop', a new activity will be started.
At each activity creation, its onCreate() will be started.
My understanding is that the singleTop option, basically finishes the caller Activity.
I think that you may think that onCreate() is a form of Application constructor but it really is an Activity constructor. You may want to do your one time initializations elsewhere.

Categories