Android Application Starts with wrong activity - android

My Application has 3 activities (MainActivity,SampleActivity,TempActivity) and Application have to start from MainActivity because i register it in AndroidManifest as
<activity
android:name=".MainActivity"
android:configChanges="keyboardHidden|orientation"
android:label="#string/app_name"
android:launchMode="singleTask"
android:screenOrientation="landscape"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".TempActivity"
android:configChanges="keyboardHidden|orientation"
android:label="#string/app_name"
android:launchMode="singleTask"
android:screenOrientation="landscape"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
</activity>
<activity
android:name=".SampleActivity"
android:configChanges="keyboardHidden|orientation"
android:label="#string/app_name"
android:launchMode="singleTask"
android:screenOrientation="landscape"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen">
</activity>
when i switch from MainActivity to TempActivity and come back to MainActivity and then close the Application then often it starts from TempActivity. after this wrong behavior when ever i start my application it starts from Wrong Activity(TempActivity). Please help me in this problem

when u go from TempActivity activity to MainActivity u give finish()
example:
Intent myIntent = new Intent(TempActivity.this, MainActivity .class);
startActivity(myIntent);
finish();

This is beacuse your activity is not destroyed, it is only paused (Check the Activity life cycle). You can override onPause to perform the desired behaviour.

How are you closing the app, means by home button application dont destroy activities, activity is stopped only. To exit your application you need to finish all of your activities in application.

Whenever you press home button you are assuming your application is closed.But it is not!!!
As per android activity life cycle you application will keep running in background and when you press application icon it will start from where it went to background.
If you are on TempActivity and if you press home button then it will start from TempActivity only.To make sure when you press home button your activity should start with MainActivity
do following changes in your code.
TempActivity.java
#Override
void onPause()
{
super.onPause();
finish();
}
So when next time you will open your application it will start with MainActivity.

Related

startActivity does not create activity the second time

I have two applications installed on my device, application A and application B.
When a user launches application A, they press a button to launch application B.
I use the following code:
Intent intent = Global.CurrentContext.getPackageManager().getLaunchIntentForPackage("some.random.app");
Global.CurrentContext.startActivity(intent);
The first time the user does this, it works fine and works as expected, however, if the user then launches the phones task manager and swipes away application B. When the user then presses the button in application A. The activity is not created.
I believe this is something to do with the way that the task and the activity are being destroyed by android. My thought process is that when the user swipes to kill application B that the activity is destroyed, but the task remains. Then the second time they press the button in application A, that it's attaching to the old task and then not showing for the user.
Application A's activity manifest for the activity:
<activity
android:name="a.a.a.LogonActivity"
android:label="#string/app_name"
android:noHistory="true"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Light.NoTitleBar"
android:windowSoftInputMode="stateHidden"/>
Application B's activity manifest for the activity:
<activity
android:name=".activity.HomeActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
When I set android:noHistory="false", it works. I think it is android:noHistory="true" killed applicationA's top activity, makes it doesn't work.

Up navigation goes to main activity

I have the following Androidmanifest file
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".First"
android:label="#string/title_activity_first"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="android.anoop.com.myapplication.MainActivity" />
</activity>
<activity
android:name=".Second"
android:label="#string/title_activity_second"
android:parentActivityName=".First" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="android.anoop.com.myapplication.First" />
</activity>
</application>
In main activity's onCreate method i have the following code to launch Second activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent1=new Intent(this,Second.class);
startActivity(intent1);
}
Other than that every other activities code is what android studio provide as default. But when i press up button from the Second activity it goes directly to main activity and not to the First activity. I know there are alternate solutions for this problem but i just want to know why the up navigation is behaving like this.I am new to android so i may not be understanding something really simple. Any help will be appreciated. Thank you.
If you are using calling SecondActivity in onCreate of FirstActivity, then it goes to SecondActivity. But think about it, when do an Activity actually starts?
It starts after you call onCreate, then onStart , then onResume and then your Actiivity finally starts running. In your case, you are not allowing to go further onCreate, and hence FirstActivity has never even started. And thats why when you go back in navigation it jumps to the previously opened Activity, i.e., MainActivity.
I hope this clears your answer! For reference see the Lificycle of an Activity in this link.
Because FirstActivity is not started or Created. You are calling Second directly from Main Activity. As there is no any First Activity available in application's stack how Second Activity goes to parent Activity First? It will goes to Main Activity as Its the only Activity remain in stack.

Android main activity does not launch on start

The main activity does not launch when I open the application; I close it by clicking on the home button. When the app starts, it opens on the same page where I was when I clicked the home button. It somehow remembers the last page I was at, and opens that page instead of the launcher activity. I added onDestroy() to onPause() on all the activities, but that didn't help either.
I am not sure what code to add here, so I'm attaching my AndroidManifest. Thanks! Below are the three activities in the AndroidManifest.
<activity android:name="example.Activity1" android:label="MyApp"
android:configChanges="orientation" android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="example.Activity2"
android:configChanges="orientation"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="example.Activity3"
android:configChanges="orientation"
android:screenOrientation="portrait" >
</activity>
When Home button is pressed, onStop method is called in your activity. So what you may do is to add finish(); in onStop method to destroy your activity.
Your activity is saved on the backstack. Play with the flags passed to the intent when you create your activity. For example, FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET will clear all the activities from this to up the backstack.
Intent myIntent = new Intent(this, MyActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
startActivity(myintent);
When you close, o exit myintent Activity, when you reopen you app it will clear delete myintent Activity from the backstack and all the activities opened from this activity.
Make sure you activity name includes you entire package name instead of example.
Or just replace android:name="example.Activity1" with android:name=".Activity1"

Android Activity Task in foreground

I have 3 activity tasks A-B-C
<activity android:name=".LoginActivity"
android:label="#string/app_name"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".DashboardActivity"
android:label="#string/app_name" >
</activity>
<activity android:name=".CreateNewPolygonActivity"
android:label="#string/app_name"
android:launchMode="singleTask"
android:alwaysRetainTaskState="true"
android:configChanges="orientation|keyboard|keyboardHidden">
</activity>
<service android:name=".services.RecordCircuitService"
android:enabled="true" >
</service>
And I have a Service with GPS using LocationManager.
My application has this logic:
On launcher icon clicked - > start login activity
After login - > login activity is finish(); and start dashboardActivity
In the dashboard I start CreateNewPolygonActivity and start the Service with GPS
Press HOME (this is reorganize RecordActivity to foreground)
after I have some mistakes work
open CreateNewPolygonActivity with onCreate
open DashboardActivity
I think I have a problem with my task organization.
You won't achieve the desired result by setting launchMode="singleTask" for your activity. Android won't create a separate task for this because you've not set the taskAffinity. In any case, you don't want to do this by using launchMode="singleTask" because this causes more problems than it solves. This launch mode is intended for HOME-screen replacements only!
Remove launchMode="singleTask"`` fromCreateNewPolygonActivity`
Also, you should remove android:noHistory="true" from your login activity. This isn't necessary either. Since you've already finished the login activity when the user starts the DashboardActivity, when he presses the BACK button, it won't return to the login activity (which I assume is what you wanted).
Once you've done these things, please tell us what is still broken.

android:launchMode="singleInstance" is making the app very slow

I have 2 activities in my app. The A activity is the launcher one. When I run the app the very first time, the launcher activity runs, but when I press the home button and restart the app from there by clicking the app icon, I always get the B activity running.
I want to make sure that the activity A should always run when starting the app.
This is the manifest code:
<application
android:icon="#drawable/icon"
android:label="#string/app_name" >
<activity
android:name="com.velosys.smsManager.Activities.a"
android:launchMode="singleInstance"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.velosys.smsManager.Activities.b" />
</application>
Using android:launchMode="singleInstance" serves my purpose,but it makes the movement from one activity to another really very slow.Can you please suggest me any option for `android:launchMode="singleInstance".Please help me.Thanks in advance.
you can try to override onUserLeaveHint() method in B activity (this will register Home button pressed event) and then create intent
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(startMain);
which isn't really an ideal solution
EDIT:
yeah sry, best solution would be that you call finish() inside onPause() method of B activity

Categories

Resources