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"
Related
I have an activity A, and when I press an toolbar item, it starts activity B using startActivity(intent). Whenever I press back button or the up navigation icon, it closes my app. I believe it's because I'm using launchMode="singleTop" in my parent activity (I'm using this because I have a Search View and a searchable configuration, because I don't want to start another instance of my activity on for search). So the question is: How can I get back from child activity(B) to parent activity(A) using both up navigation and back button without closing my app? I've searched about it, and I found something about onNewIntent(). If this is my solution, how should I use it properly?
Here is my manifest file:
<activity
android:name="com.example.fernando.inspectionrover.MainActivity"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
<activity
android:name="com.example.fernando.inspectionrover.BluetoothSettingsActivity"
android:parentActivityName="com.example.fernando.inspectionrover.MainActivity"
android:screenOrientation="landscape">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.fernando.inspectionrover.MainActivity" />
Here is how start my new activity:
switch (id) {
case R.id.bluetoothActivity:
Intent switchActivity = new Intent(this, BluetoothSettingsActivity.class);
startActivity(switchActivity);
Log.i(LIFE_CYCLE, "Switching from " + getLocalClassName() + " to Bluetooth Setting Activity");
finish();
break;
}
Single Top means that if you launch an activity that is already on top, it wont be created again just resumed.
The reason your back navigation close the app is because you are calling finish() just after you start a new activity. Which means you don't need that activity anymore so it is removed from the stack. If you go back on activityB, the app will close because there is nothing to go back (you called finish() remember?
I may be just poking at the easiest answer but I think the main problem is that you are calling finish after you start the new activity. This calls on destroy for the calling activity and removes it from the activity stack.
In my app I have a Activity that is opened from a Notification, but is not declared has main activity or launcher activity·
Also, this activity is declared has "singleTop".
Works fine for most users, but some are a little problem.
In some occassions when click the Notification the Activity is opened and, when click back, the app Main Activity is shown.
How to don't show the main activity when click back?
Also, if the users are in this activity and click home, and later it clicks into the app icon in order to open the Main activity, the previously opened activity, that is not main activity is opened. This can change?
Thanks.
-- The structure of the manifest is:
<activity android:name=".MainActivity" android:label="#string/app_name" android:exported="true" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ShowAlarmsActivity" android:launchMode="singleTop" android:exported="true" android:theme="#style/AppThemeWithoutActionBar" />
you may do this my either overriding on back press and starting the parent activity using this code
or if you are using action bar you may use parent activity tag
Intent intent = new Intent(getApplicationContext(), ParentActivity.class);
startActivity(intent);
also if you want to distinguish that if the user is coming from notification or from app launch you may use intent to pass some key value and cater it
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="parentActivity" />
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
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.
I'm expecting quite a strange behavior in a tiny app I'm currently working on.
The app consists of two activities. From the first activity I'm launching the webbbrowser via an intent. When I press the back key in the browser it returns to the SECOND activity even if I manually closed the app previously before launching.
1) First Activity
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(mWebShopURL));
startActivity(intent);
First Activity launches second activity like this
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
Bundle b = new Bundle();
b.putString("product", mProduct);
intent.putExtras(b);
startActivity(intent);
2) Second Activity -> first activity
onBackPressed();
AndroidManifest
<activity
android:name=".FirstActivity"
android:label="#string/app_name"
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=".SecondActivity"
android:label="#string/app_name"
android:screenOrientation="portrait" >
</activity>
If you want the second activity to complete and go away when the user taps on the simple "back button" you refer to in your comment, then don't call onBackPressed(). Please just call finish(). That will make the second activity go away and return the user to the first activity.
Your problem is that you've never removed the second activity from the stack, which is why returning from the browser shows that activity.
If i am correct You are overriding onBackPressed() in your second Activity.Dont do that put onBackPressed() code in comments and try. Hope it will help :)