how to start application in last viewed activity in previous session

by greg » Thu, 22 Apr 2010 02:30:08 GMT

Sponsored Links
 I'm writing an application consisting of several activities and, on
restarts, I'd like the application to initially display the
application's activity that was the last to display in the
application's previous session.

I save information about the last activity in SharedPreferences and
I'm trying to start that last activity with a call to
startActivity(intent) from my Application's onCreate handler.  The
problem is that if I don't set the intent's FLAG_ACTIVITY_NEW_TASK,
the result is a run-time exception message in logcat suggesting that I
should set the flag.  And if I set that intent flag, the correct
activity gets started but doesn't fully initialize resulting in null
pointer exceptions (which don't occur when I don't attempt to start
the activity from my Application class).

- - -

W/dalvikvm(  805): threadid=3: thread exiting with uncaught exception
E/AndroidRuntime(  805): Uncaught handler: thread main exiting due to
uncaught exception
E/AndroidRuntime(  805): java.lang.RuntimeException: Unable to create
application com.test.tangled.TApplication:
android.util.AndroidRuntimeException: Calling startActivity() from
outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK
flag. Is this really what you want?

- - -

Any tips on how to correctly start a particular activity from the
Application class's onCreate handler?



Other Threads

1. PreferenceScreen - <intent 鈥? > - Exception - FLAG_ACTIVITY_NEW_TASK

I've got a probleme with an exception, but I don't know why I get this

The project is about implementing an AbstractAccountAuthenticator with
an AbstractThreadedSyncAdapter to sync certain data on a server with a
content provider.

I made everything and both adding accounts and syncing is running
without any problems.

Now I've tried to add a Preference-Screen just like suggested in the
android reference (search for AbstractAccountAuthenticator, it is
explained there) via xml like this:


<account-authenticator xmlns:android="

The Preference-Screen-xml looks like this:


<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="
 <PreferenceCategory android:title="@string/pref_cat_general">
  <PreferenceScreen android:key="@string/pref_cat_scr_keygen"
   <intent android:action="android.intent.action.VIEW"
android:targetClass="" />

The screen is promted how it should be, but and that is the problem:
When I click on the PreferenceScreen with the intent it leads to a
crash of the system (both in the emulator and my HTC-Desire).

Logcat says:

    ERROR/AndroidRuntime(18754): android.util.AndroidRuntimeException:
Calling startActivity() from outside of an Activity context requires
the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

(whole Logcat on pastebin:

Here the part of the manifest, where the activity is defined:

<activity android:name=".KeygenAct">
     <action android:name="android.intent.action.VIEW" />
     <category android:name="android.intent.category.DEFAULT" />

The whole test-code (it is my project on the essentials to find the
problem) is to find at googlecode (
cleanproject/source/browse/) (Eclipse-Project)

Thank you already for your help, Esentian

p.s.: keygen is not a key generator, it means more key_general. A bit
inartfully expressed ;)


2. How to calculate distance between to city (dynamically)

Hi All,

I want to calculate the distance between 2 city, whatever user

Example -
Default City - 1st City - New York or New Delhi, London Seoul anything
User Put     - 2nd City - California

Need - to calculate distance between California to New York  or London

Welcome any hints


3. Geocoder backend service

4. Activity throws an exception after the 1st run

5. Navigate up activity stack

6. Using a different layout depending on parent dimensions

7. Moved Android project to Eclipse 1.5 and new machine and project is missing Android jar