How to preload Activity? - android

My aar has an Activity, and when I call the API, it creates an Activity every time.
also use the startActivityForResult function to get a response from the Activity.
button1.setOnClickListener(new Button.OnClickListener() {
Intent myActivity= new Intent(getApplicationContext(), myActivity.class);
myActivityOption option = new myActivityOption();
option.setType(2);
myActivity.putExtra("myActivityOption", option);
startActivityForResult(myActivity, REQUEST_CODE);
}
When MyActivity is created, it takes 2-3 seconds to draw a lot of images.
I want to make the delay occur only once.
I would like to be able to create an Activity without delays after initial init in the same form as class.
I'm sorry I missed my English.

Related

Run second activity when first is closed

Can you tell me how to run my second activity if my first activity is closed?There can be some time before the second activity must be opened.Should I use a Service?And please give an example.Thank you so much.
The answer will depend under what condition you want to start the next activity. Following is just a sample, whatever is done in the onDestroy can be copied anywhere else to start the next activity.
Override the onDestroy method of the first activity and then fire an intent to start the second activity.
In your first activity,
public class A extends Activity{
.
.
.
#Override
public void onDestroy(){
Intent in = new Intent(this, SecondActivity.class);
startActivity(in);
.
.
.
}
}
Make sure both the Activities are metioned in the AndroidManifest.xml
EDIT: To start after some time, you can use a Handler like this. (NOTE : This one is not tested. I hope it works as expected)
public void onDestroy(){
new Handler.postDelayed(new Runnable(){
public void run() {
Intent in = new Intent(this, SecondActivity.class);
startActivity(in);
}
}, 7000);
}
7000 over here is the milliseconds after which you want to start the next activity. i.e the next activity will start after 7s.
All the best :)
if my first activity is closed
For me it sounds like you mean "closed as a result of a user action". Starting a new Activity from onDestroy() is a bad idea since the system may destroy your Activity if it needs to. It can also be destroyed if the device is rotated. A better approach is overriding the back button:
#Override
public void onBackPressed() {
//delay 2nd Activity launch
super.onBackPressed();
}
If you try to delay a launch of a second Activity from within your first Activity, you risk it not opening at all, if the app is cleared from memory. Your best bet is to use AlarmManager to schedule the opening of a new Activity after a certain amount of time. This puts the work on the Android OS instead of in your app.
This post contains some code examples of how to approach this.

Passing current Intent as extra to another Activity

I have a problem with my Login screen. When it's started, I check for network connection, and if it's disabled, I want to show NoNetworkActivity. And the same for every other screen: when Activity is launched, I check network connection and navigate to NoNetworkActivity is needed. When navigating, I want to save the Intent which launched this previous activity and finish it to disable the Back button redirection when on NoNetworkActivity. So, when connection is restored, I want to launch that intent and get actual state of the app before this error:
LoginActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
if (!App.getInstance().isNetworkConnected()) {
Intent noNetwork = new Intent(this, NoNetworkActivity.class);
noNetwork.putExtra(NoNetworkActivity.EXTRA_FAILED_INTENT, getIntent());
startActivity(noNetwork);
finish();
}
...
NoNetworkActivity
private void checkNetworkConnection() {
mCheckButton.setVisibility(View.INVISIBLE);
mProgressBar.setVisibility(View.VISIBLE);
if (App.getInstance().isNetworkConnected()) {
Intent failedIntent = getIntent().getParcelableExtra(EXTRA_FAILED_INTENT);
startActivity(failedIntent);
finish();
} else {
mCheckButton.setVisibility(View.VISIBLE);
mProgressBar.setVisibility(View.INVISIBLE);
App.toast("Connection failed");
}
}
And it's getting strange: startActivity(failedIntent) does NOTHING. I've tried to remove finish() from next line, and NoNetworkActivity just stays on top without anything happening.
And one more thing. You can suggest passing Activity actual class names instead of intents, but I realy need Intent. That's because I'm using a lot of starting actions for every activity and a bunch of extras.
Thanks in advance for any help. Cheers!
Very bad approach. Don't use it.
First, you don't need to finish previous activity just to disable Back action. You can override onBackPressed().
Second, you don't need to start parent activity again. Just call a new activity with startActivityForResult(); and override onActivityResult() callback.
Third, but most important. Why do you want to call a new activity just to show 'No Network' message? And what if network won't be re-established? Just create isNetworkEnabled() method and call it when user attempts to get data from the Internet, before sending actual request to server. If no network - notify a user with an alert or toast.
I suggest you use fragments instead of activities first of all.
Using fragments you can set retainInstance(true);
To disable coming back from an activity to the previous :
1)call finish() on that activity
2)
Intent i = new Intent();
i.setClass(this, MyActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);`
It works with an explicit Intent.
In LoginActivity substitute:
noNetwork.putExtra(NoNetworkActivity.EXTRA_FAILED_INTENT, getIntent());
with:
noNetwork.putExtra(NoNetworkActivity.EXTRA_FAILED_INTENT, new Intent(this, LoginActivity.class));
Btw, Alexander Zhak has some good points in his answer

Android intent service onClick, or start new activity and then Asynctask

Many times in android apps, users click a button/view and start a new activity, where the new activity pops up in front of what the user was previously looking at, and loads data.
Would there be any difference from a user perspective if the data started loading (from network or disk or both) when the user clicked the button before the next activity started. And then that data was returned to the new activity in a broadcast receiver.
This is compared to starting the process in the oncreate of the activity. Assuming these network and i/o processes only take milliseconds either way, would it make a difference to the user if the methods were started in onCreate of the new activity, or started in the old activity onClick.
First way, starting I/O and changing views after I/O finishes
//first activity
public void onClick(View v){
startActivity(new Intent(this, NewActivity.class);
}
//NewActivity.class
onCreate(Bundle mBundle){
super.onCreate(mBundle);
setContentView(R.layout.mView);
mObject = networkCall(); //after network call, the view objects in this layout will reflect data from the network call
}
second way, starting the I/O in the first activity
//first activity
public void onClick(View v){
IntentService networkCall = new IntentService();
//start network call
startActivity(new Intent(this, NewActivity.class);
}
//second activity on create just sets the view and also broadcast receiver
My GUESS is that in the split second that it takes for the activity to pop up, the data from the intent service could become available. But at the same time, passing data via intent could take just as long making the benefits marginal
Insight appreciated
In my experience the onCreate() of your new activity is called almost instantly from when you call startActivity(). The new activity doesn't show up right way because it has to take time to render your layout.
You could play around with timings yourself by using the Log.d() function. Something like Log.d(TAG, "This happend at: " + System.currentTimeMillis()); at different points in your code to see when things happen. Watch the LogCat while your apps runs and you can decide for your self which way is better.

Android New Intent New Screen

I am still not completely sure of this opening a new screen with a new intent. I have two problems. 1 is getting it to work and the second is more theory.
Firstly I have two packages com.quiz.max and com.reason.max both have activities names accordingly eg Quiz and Reason respectively. Here is the on click code I am trying to execute at the moment in quiz to go to reason.
Intent intent = new Intent();
intent.setClassName("com.reason.max", "com.reason.max.Reason");
this.startActivityForResult(intent, requestCode);
Secondly I heard if I start this intent then everytime i click the button a new intent is created. Does this mean if the user goes to reason page and navigates back and clicks the button again they actually create a new intent instead of going back to the already active one. Thus dozens could be opened via this method. Therefore should I close each reason intent once navigated back or is this a redundant point?
Max
I think you want
Intent intent = new Intent(this, Reason.class);
startActivityForResult(intent, requestCode);
Secondly, you don't "start an intent". You use an intent to ask an Activity to start, in this case the Reason activity. And yes, the default behavior is to start a new instance of the activity each time it is requested.
You can alter this behavior with the launchMode.
Make sure you read and understand the Activity lifecycle. You don't need to worry about too many Activities in existence, Android will handle that for you, but you should properly save state and clean up connections in the appropriate lifecycle methods.

Failing with multiply instances

I`m trying to launch an Intent filter from one process to run an activity of another proccess.
This is pretty easy:
protected void startIntent()
{
Intent i = new Intent(target);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(i);
}
Now as my topic says, I am willing to run the target activity as multiply instances.
Each instance of that activity is supposed to do some operation and then it has finish();
So now I call startIntent(); 4 times in a row.
I want to have 4 instances parallel in the stack of the same target activity (each activity will finish after it does some operations. with finish(); ).
The problem is that it doesn't work properly. I tried some LoadTest, and when I call the method 3 (or below) times in a row it works fine, but when I call startIntent() 4+ times some instances of that activity never get launched(for example the second Intent in the order that has been called).
This is some warning I get:
WARN/ActivityManager(966): Duplicate finish request for HistoryRecord{4367ed60 {com.test.targetActivity/com.idan.external.callingActivity}}
Btw: In the target activity I've setted in the manifest the flag:
android:launchMode="standard" (also tried without it, and it didnt work).
Any idea?

Categories

Resources