Android Transition between Activities from non-Activity Class - java

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);

Related

Activity to be closed from that activity itself in Android studio

I have 2 Activities, say activity A and activity B. I have to call activity B from the activity A. Now that is done by using Intent. There is some code in the activity A that must be executed after activity B ends. How can that be done?
I use the following code :
Intent intent = new Intent(A.this, B.class);
startActivity(intent);
finish();
How can i accomplish that?
According to Android Docs: "Starting another activity doesn't have to be one-way. You can also start another activity and receive a result back. To receive a result, call startActivityForResult() (instead of startActivity())."
Activity For Result Doc
And here you have a question about Activity For Result where you will find an example.

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.

intent flag over intent startActivity(i);

#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()

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.

Code to Switch to another java class on android java

I have a java class that loads the first xml layout and the second java class that loads another xml. layout
If I am about to switch from my first java class to the second one using code. How would I do it?
You have to use intents to switch activities.
You can create a new Intent that specifies the class to launch, and fire this intent. This will load your other activity.
Something like:
Intent intent = new Intent(this, OtherClass.class);
startActivity(intent);
Assuming you mean start one Activity from another, you would do the following in Activity1:
Intent intent = new Intent(this, Activity2.class);
startActivity(intent);

Categories