I am trying to use the next code to return back to the My Application Home Activity from the stack of my application:
protected void goHome(boolean offlineMode) {
final Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
...
}
But in this case all other applications activities on the top will be closed too.
Is it possible to close activities only of my application in this case?
This should do the work.
Intent i = new Intent(context, HomeActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
finish();
You can also implement this from your AndroidManifest.xml file, just adding
android:noHistory="true"
attribute in those <activity> you want (eg. HomeActivity)
see here is the sample code of manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rdc.helloWorld"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".HelloWorldActivity"
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>
</application>
</manifest>
so when you click back button on Home Activity screen, it will clear history of this screen only
You may like read about noHistory tag details,
How does android:noHistory=“true” work?
Related
I have a small app, then when I get out of the app (minimizo) and open again, it starts with the application of the home screen instead of opening the page you left off. That is, it starts the application all over again when it is minimized.
<?xml version="1.0" encoding="utf-8"?>
package="com.example.kevin.estudosbiblicos" >
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#mipmap/icon"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden"
android:label="#string/app_name">
</activity>
<activity android:name=".Splash">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
First, you need to finish the splash activity after starting mainActivity.
new Timer().schedule(new TimerTask() {
public void run() {
Intent intent = new Intent();
intent.setClass(Splash.this, MainActivity.class); //Chamando a classe splash e a principal (main)
startActivity(intent);
finish();
}
}, 2000);
And in mainActivity , can you call finish() method in onpause method.
please show the whole mainActivity code
and you shouldn't use finish in onpause in your activity.
<activity ...
android:alwaysRetainTaskState="true"/>
Do this for all activities you want to save the state.
I am having an issue when trying to go from the main screen of my app to display the data that i have collected in my SQLiteDatabase. Here is my Application manifest...
I have tried switching the Main.java file from LAUNCHER to DEFAULT and the opposite for the database java file to make it open when it is run and i can get it to display that class that way but not using the button that I have in the Main.java file that is for changing to see the data.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.innovativesolutions.gpsareafinder"
android:versionCode="2"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="14" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.innovativesolutions.gpsareafinder.Main"
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.innovativesolutions.gpsareafinder.Locationdbview"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Here is the section in my Main.java file that has to do with the Intent where I am trying to call the Location database java file.
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bcalculate:
area();
break;
case R.id.bclear:
clear();
break;
case R.id.bloc:
Intent in = new
Intent("com.innovativesolutions.gpsareafinder.Locationdbview");
startActivity(in);
break;
I think the error is something simple in my manifest or where I try to use the Intent to switch views...If anyone has any ideas please help. Thanks!
Change your second activity in the manifest to:
<activity
android:name="com.innovativesolutions.gpsareafinder.Locationdbview"
android:label="#string/app_name" >
</activity>
You don't need any filters there.
Change the call in your firstActivity to:
startActivity(new Intent(this, Locationdbview.class));
You can also try this in Main.java
Class myclass =Class.forName("com.innovativesolutions.gpsareafinder.Locationdbview");
Intent in=new Intent(Main.this,myclass);
startActivity(in);
In Manifest.xml :
<activity android:name=".Locationdbview" ></activity>
In MainActivity, I call SincActivity.
This screen has a "start" button, a TextView and a ProgressBar to show the status.
The button start an AsyncTask. This AsyncTask updates the components on the screen, and also in the notification area.
The notification area can be clicked. I passed into their creation which Activity it should open. I have spent SincActivity.
When the user is on the screen SincActivity and click the button, all components are updated as expected.
When I click on the notification area, android opens a new screen SincActivity, with initial values.
It's as if somewhere, android execute: "new SincActivity";
What I need is to be shown always the same screen.. the same instance. Almost like a singleton.
Is this possible?
UPDATE
AndroidManifest.xml
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="br.com.rokko.gabinetemobile.MainActivity"
android:label="#string/app_name"
android:configChanges="orientation|keyboardHidden|screenSize"
android:theme="#style/FullscreenTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="br.com.rokko.gabinetemobile.ModeloActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/title_activity_modelo"
android:theme="#style/FullscreenTheme" >
</activity>
<activity
android:name="br.com.rokko.gabinetemobile.SincActivity"
android:label="#string/sincronizacao" ><!-- android:launchMode="singleInstance" don't work -->
</activity>
</application>
Try adding android:launchMode="singleInstance" to the <activity> tag for this Activity in your AndroidManifest.xml file.
If you just want the notification to bring your task to the foreground, without creating any new instances of activities, you need to use a "launch Intent" in your notification. Something like this:
Intent intent = new Intent(this, MainActivity.class);
// Set the action and category so it appears that the app is being launched
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
use pendingIntent when you create your notification.
For more information, see Android application current activity bring to front issue
I've been looking ALL DAY to no avail..I know there are several questions on this but I haven't been able to find one that works properly for me.
Basically, I have an activity (MainActivity), with a function to add a shortcut to the home screen. When I click the shortcut, I want it to open a different activity (NewActivity). The hold up here seems to be that it doesn't work right when I try to launch a different activity than the one from which the shortcut was made.
The shortcut is successfully made, but when I click it I get an "App isn't installed" toast message from the Android system.
Here's the code for adding the shortcut:
Intent shortcutIntent = new Intent();
shortcutIntent.setClassName("com.enceladus.myApp", "com.enceladus.myApp.NewActivity");
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Calculator");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(MainActivity.this, R.drawable.calc));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
MainActivity.this.sendBroadcast(addIntent);
EDIT: Here's my manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.enceladus.myApp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.enceladus.myApp.NewActivity"
android:label=""
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Translucent.NoTitleBar"
android:launchMode="singleInstance"
/>
</application>
NewActivity.class functions perfectly (I know because I've debugged it a lot), so that's not the problem.
Try making your manifest look like this for the NewActivity entry:
<activity android:name="com.enceladus.myApp.NewActivity"
android:label=""
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Translucent.NoTitleBar"
android:launchMode="singleInstance"
>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
</intent-filter>
</activity>
also maybe try making the shortcut intent like this:
Intent shortcutIntent = new Intent(MainActivity.this, NewActivity.class);
shortcutIntent.setAction(Intent.ACTION_MAIN); //Not sure if you'll need this or not.
//remove the next line.
//shortcutIntent.setClassName("com.enceladus.myApp", "com.enceladus.myApp.NewActivity");
The other way that you can do this is by setting the android:exported flag to true.
Intent intent = new Intent();
intent.setClassName("another_app_package_name","another_app_package_name.class_name_in_that_package");
startActivity(intent);
getting ActivityNotFoundException ?
How to solve this problem? This problem posted earlier as well but no solution.I manifest file of application whose activity to be called i have used intent filter as well.
Activity to be called by some other application's activity
<activity
android:name="com.example.custompermission.PrivActivity">
android:permission="abc.permission.STARTMYACTIVITY">
<intent-filter >
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
Check this
Intent intent = new Intent();
intent.setClassName(another_app_package_name.this,another_app_package_name.class_name_in_that_package.class);
startActivity(intent);
and in manifest.xml add this
<activity android:name="another_app_package_name.class_name_in_that__package" android:configChanges="orientation|keyboardHidden"></activity>
In manifest file you do like this:
<activity android:name="another_app_package_name.class_name_in_that__package"></activity>
you should add the activity in manifest file, which you are tried to called from first activity.
you need to add both the activities in manifest file:
for ex:
Intent intent = new Intent (HomePage.this, Second.class);
startActivity(intent);
in manifest: <application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".HomePage"
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 =".Second"></activity>
</application>
Once, clean the project and run.