When I execute the code the startActivity() is called only after the startActivityForResult() is over. How can I start the startActivity() first? I tried with threads but I didn't succeeded.
// Splash Correct
Intent correct = new Intent("com.quizcontest.alex.SPLASHCORRECT");
startActivity(correct);
Bundle b = new Bundle();
Intent i = new Intent(StartPlaying.this, CorrectAnswer.class);
b.putInt("p1Key", player1Score);
b.putInt("p2Key", player2Score);
b.putInt("rKey", round);
i.putExtras(b);
startActivityForResult(i, 0);
startActivity does not block. It causes something to happen in a new thread, so it will immediately execute the lines that happen after it.
It seems like you are trying to show a splash screen. See this other question related to spash screens: Android SplashScreen or this example for displaying a splash screen using a dialog: http://blog.iangclifton.com/2011/01/01/android-splash-screens-done-right/
If the behavior you want is to start activity 1, and then start activity 2, the correct behavior is to start activity 1 for result. Then onActivityResult will be called when activity 1 has completed. At this point you can start activity 2.
Related
My app has three fragments inside main activity, just like whatsapp.
This is the intent I use to go from one of the fragments to a new activity "A".
Intent intent = new Intent(context, GroupChatActivity.class);
intent.putExtra("currentGroup", key);
context.startActivity(intent);
From "A", it goes to another new activity, "B".
I got the below ERROR when I pressed back button inside activity, "A".
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{4758b98 position=15 id=-1, oldPos=0, pLpos:0 scrap [attachedScrap] tmpDetached no parent} androidx.recyclerview.widget.RecyclerView{d5047bd VFED..... .......D 10,246-710,1454 #7f08018c app:id/recyclerChatView}, adapter:com.example.vote.QuestionModelAdapter#87172c0, layout:androidx.recyclerview.widget.LinearLayoutManager#9c48bf9, context:com.example.vote.GroupChatActivity#7c06ca2
I have solved that error by calling finish(), overiding onBackPressed function.
But I got the same error when I pressed back button inside activity "B". It is supposed to open the previous activity "A".
(By the way, activity A has a recycle view.)
Calling finish() isn't working in here. Can you explain the above error and when it causes?
And what happened to the current activity when you call startActivity(intent)? I think something happened to activity "A" when intent is called.
I've been tweaking a lot with android design and capability of Android to use the maximum potential of an Android framework, I have come across transition, and my question is how to define Activity's Destroyed's animation ?. Say I am starting an activity using intent like so :
Intent intent_info = new Intent(ComponentsPage.this, SecondActivity.class);
startActivity(intent_info);
overridePendingTransition(R.anim.slide_up, R.anim.no_change);
That snippet basically opens up SecondActivity with Slide Up transition. Now i am in second activity and second activity say doesn't have any button but i want whenever Second Activity is closing (Destroyed) it close with slide down animation.
I've tried adding
overridePendingTransition(R.anim.no_change,R.anim.slide_down);
Inside onDestroy() and onStop(), but still no luck, i guess the activity is already closed when those methods are called.
When you try to finish the second activity try to override the finish method inside your SecondActivity as follows:
#Override
public void finish() {
super.finish();
overridePendingTransition(R.anim.no_change, R.anim.slide_down);
}
Let me explain detailed; I have notification and this notification opens B activity with two cases.
Cases :
If app is closed. (not running on background)
If app is opened. (on background or front)
Case-1
I click to the notification and it opens the B activity with case-1. When i press back i want to go to the A activity and kill B activity. I dont need B activity anymore. Everything easy from here without using flags. When I'm on B activity and press back two times from here, it goes A activity and then closes the app. My trouble here is, if i open the app from navigation buttons of phone (can't remember the name of this button) app is opening from B activity. That's not what i expected. I want to open A activity. Don't want to see B activity anymore.
Case-2
I click to the notification and it opens the B activity with case-2.When i press back i want to bring A activity to the front, without creating anything new. If i press back on B activity, two times and close the app and then again re-open app from navigation button of phone, want to open app from A activity.
So how can i make this correctly, i tried to use flags (i already read docs) but couldn't get work.
What flags should i use when i open the B activity and onBackPress method of B activity to go A activity as i wanted
This should be achievable by adding
android:launchMode="singleTask"
to the A activity in the Manifest, then you can just open A activity from B activity onBackPressed and you will have A only once in the stack.
If it's not working the way you want, you can create an abstract class that extends Activity and handle the stack in a static object, then A & B must extend this new class
try this
Intent intent = new Intent(context, YourActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
try this
android:launchMode="singleTask" in android manifest file
You can achive this by adding FLAG_ACTIVITY_REORDER_TO_FRONT
Intent i = new Intent(context, Activity.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
You May try this isTaskRoot() Which will return B is root
if it is true then launch A
other wise you may finish B
B Activity
#Override
public void onBackPressed() {
if (isTaskRoot()) {
//call A which is not exist
Intent i =new Intent(B.this,A.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}else {
//Finish B if A Already Exixt
finish();
}
}
You can call B Activity on Notification Click
if A is present then u can finish B else you can launch A
If set FLAG_ACTIVITY_CLEAR_TOP, 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.
Just put this in ActivityB onBackPressed:
Intent i = new Intent(ActivityB.this , ActivityA.calss);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
finish();
How solve case 1:
finish(); on ActivityB BackPressed method make ActivityB finish after open ActivityA. So after opens ActivtyA, ActivityB will shut down.
How solve case 2:
With this combination flag, It will do what you want. It will close all activities in stack and just keep destination activity. If instance of activity exist it will use it and calls OnNewInstance and if not it will creates new one.
If this is the only instance of Activity B being used, you can add the flag noHistory to the manifest for Activity B
android:noHistory="true"
This will stop Activity B being added to the back stack, this is also possible dynamically by using the Intent Flag FLAG_ACTIVITY_NO_HISTORY when calling Activity B.
As for having Activity A start when Activity B is killed #Quentin Menini's answer of having a single task activity set in the manifest will work if that is the only way you wish Activity A to be accessed, or the Intent Flag Intent.FLAG_ACTIVITY_REORDER_TO_FRONT as #Naimish Vinchhi has suggested, will have the desired effect in this instance.
https://developer.android.com/reference/android/content/Intent.html
link to see all possible Intent flags
https://developer.android.com/guide/topics/manifest/activity-element.html
link for all possible manifest activity options
How to prevent to create every time Instance of Store activity?
When I call startActivity(new Intent(this,StoreActivity.class)), it will create new instance and call OnCreate Method in StoreActivity. I want to call one time Oncreate.
Is this Possible ?
Do this
startActivity(new Intent(
this,StoreActivity.class).setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP));
From Android Documentation
public static final int FLAG_ACTIVITY_SINGLE_TOP
If set, the activity will not be launched if it is already running at the top of the history stack.
Constant Value: 536870912 (0x20000000)
Start your activity like this:
Intent storeIntent = new Intent(this, StoreActivity.class);
storeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(storeIntent);
It will call onCreate() only once on first launch of activity, if activity is already running it calls only onNewIntent() instead of create new instance of activity or calling onCreate.
This is not possible since each time you press back button, onBackPressed() method is called which actually destroys your StoreActivity.
If you want this method to not destroy your activity, just remove or comment out super.onBackPressed() line in this method.
In this case your activity will not be destroyed when back button is pressed, but then you will have to use any other logic to bring your MainActivity to top of the stack.
try to finish the first activity after starting the new one,
add this code after the Intent command.
finish();
like that:
startActivity(new Intent(this, newActivity.class));
finish();
check this link
Finish an activity from another activity
Old Title: Continue execution of code from activity 1 only after activity 2 finish()
Let's consider this code:
// Call activity to take some pictures
Intent i = new Intent(MainAct.this, CameraAct.class);
startActivity(i);
// After CameraAct is closed (finish()) then send pictures to webservice
sendPicturesToWebService();
I know this code is wrong because after run startActivity(i) the code will not wait until that new activity finishes to run sendPicturesToWebService() like if the new intent was a model window/screen.
How can I get this done?
by 'this' I mean: create and show a new activity as if it was a modal window / a dialog window; and continue to execute code from old activity right after the modal activity is closed.
you have to startActivityForResult() and then handle the "result" on the onActivityResult callback. Here the link reference to the official docs:
startActivityForResult
onActivityResult