Restore activity - android

When I rotate the phone the activity calls again onCreate and reset everything.
Is it possible to keep data and services like the location manager or I have to save and restore it?
By data I mean something like a list of objects.
How can I prevent code from keep running onCreate every time I rotate the phone?

Add this to your Activity in your AndroidManifest.xml:
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
Example snippet:
<activity
android:name="your.package.activity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" <!-- Keep data on rotation -->
android:windowSoftInputMode="adjustPan"> <!-- Keep keyboard from "pushing" layout -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Note: This is not the "preferred" way but I use it most of my Android applications(Read here: http://developer.android.com/training/basics/activity-lifecycle/recreating.html)

Related

Activity orientation restore

There is an Activity named PlayActivity which is used to play videos, and I set this activity to use the landscape model by xml:
<activity
android:name=".activity.PlayActivity"
android:screenOrientation="landscape">
Now once I enter the activity from another Activity say it is MainActivity, the PlayActivity will use the landscape model as expected, however once I hit the back button to return to MainActivity, I found that the MainActivity is changed to landscape too, how to avoid this?
Try to put your MainActiviy activity in the portrait, since you can configure the whole application in one direction to customize each one of them to leave an example of how serious greetings.
<activity
android:name=".activity.MainActivity"
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=".activity.PlayActivity""
android:screenOrientation="landscape"
/>

Application icon does not open last used activity

In some situations (I do not know exactly what causes this to happen),
if I start my app, open another (here AaaActivity) activity and then click the home button to get to the phone home view, and then click the app icon from there, it starts in the application home view, instead of starting in the last open activity.
Why is this and what do I need to do to make it return to the last used activity (AaaActivity)?
Here is an excerpt from the manifest:
<activity
android:name="x.y.app.android.SsssActivity"
android:theme="#style/NoTitleBarNoBackground">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="x.y.app.android.AaaActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="#string/activity_videoview"
android:launchMode="singleTop"
android:screenOrientation="sensorLandscape"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="x.y.app.android.AaaName" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Multiple tasks can be held in Android OS in the background at once. However, if the OS is running many background tasks at the same time, the system might begin destroying background activities in order to recover memory, causing the activity states to be lost. This is OS design and you can't prevent it. What you can do is saving your state so that you can retrieve it next time. Tasks in Android

Every Activity of my application is shown as an app on its own

Just like the title says, for some reason which I dont understand, Every Activity I create in my application is shown as an icon for an application on the phone's app menu.
Could someone please help me solve this wierd problem?
If you need some code just ask for it (I dont know which code is related with these kind of stuff).
Thanks!
In your manifest remove these lines from all Activities except the one you want to open on:
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
Example:
<activity
android:name="your.package.name.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<!-- This line declares this Activity as the start point -->
<action android:name="android.intent.action.MAIN" />
<!-- This line adds a launcher icon -->
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="your.package.name.OtherActivity"
android:label="#string/app_name" >
</activity>
In your manifest file you must have set launcher intent-filter for all of your activities.. Keep launcher intent-filter only in launcher activity..

How to change the startup activity in android?

I have two activities namely login and calendar in my Application. Currently my startup activity is "calendar". I want to run the login as first activity not calendar.
The startup activity [Launcher Activity] is declared in the projects' AndroidManifest.xml file
Look for that activity tag in the manifest which looks like this
<activity android:name=".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>
Look at the attribute android:name. Main is the class which is launched when the app starts. Currently your calendar activity name should be there. Change that to the .classpath of your activity that you want to launch.
That should do it. You may also want to do the hello world application in the tutorials and go through the docs a little to see how Android Applications work.
Add Intent filter to the Activity in which you want startup.
In your case Modify the AndroidManifest.xml file as follows
<activity android:name=".login"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
remove the intent-filter code from calendar Activity tag in manifest and add it to the Activity you wanna load first
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
I mean paste it in the activity you like to run as default.
<activity
android:name="com.example.gridviewimages.AnotherActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Where as
From the docs
category -- Gives additional information about the action to execute. For example,
CATEGORY_LAUNCHER means it should appear in the Launcher as a top-level application, while
CATEGORY_ALTERNATIVE means it should be included in a list of alternative actions the user can
perform on a piece of data.
MAIN means that this activity is the entry point of the application, i.e. when you launch the application, this activity is created.
You want the Application element of the Android Manifest file. You can see details here.
Look at the name attribute, this points to the Application class.

Why its my widget updating on the configuration activity creation?

i have set it to update each 12 hours :
android:updatePeriodMillis="43200000"
then in the manifest I have this:
<application android:icon="#drawable/icon" android:label="#string/app_name">
<!--
*************************************************************************************************
* Provider Configure Activity
*************************************************************************************************
-->
<activity android:name=".ConfiguratorActivity"
android:label="Configure Widget"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>
<!--
*************************************************************************************************
* Widget Provider Receiver
*************************************************************************************************
-->
<receiver android:name=".WidgetProvider">
<meta-data android:name="android.appwidget.provider"
android:resource="#xml/widget_provider" />
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<service android:name=".AppWidget$UpdateService" />
</receiver>
</application>
however the application keep updating each time I create the widget itself when i start the configuration activity but it does not update when i click the ok button wich its the metod that have the forcing widget instructions =/
Widgets always get updated when you create them so that you can prepare the proper look. This is a good thing; otherwise your initial layout (as defined in the xml) would be displayed until it finally refreshes after 12 hours. If you don't need that you have to detect that it's the first refresh (write a flag to shared prefs) and then ignore this one.
Button: how did you connect the button to force a refresh?
Since Donut (Android 1.6) the minimum & maximum time to update the widget is 30 min, mainly to avoid your battery will be consumed shortly.
Android guys have to change their documentation:
https://developer.android.com/reference/android/appwidget/AppWidgetProviderInfo.html#updatePeriodMillis

Categories

Resources