1

I have an IntentService that runs every 5 minutes. This service checks if there are any Alerts on the server. The Alerts are in a TwoDimensional ArrayList which i pass to an Activity to display in a list.

At the moment if i start the app and let it run for an hour, there will be 12 Activities on the stack. The user would have to click the back button 12 times to dismiss all the activities.

What i would like to happen is the new ArrayList gets passed to the original Activity on the stack and it's listView gets updated with the new data.

I'm sure i would have to override onNewIntent and call notifyDataSetChanged on the adapter.

Has anyone any ideas how to do this? Am i on the right lines?

public class ShowAlertsIntentService extends IntentService{


    private static final String TAG = ShowAlertsIntentService.class.getSimpleName();
    RROnCallApplication rrOnCallApp;
    DateTime from;
    DateTime   to;
    TwoDimensionalArrayList<String> alertsArray;

    public ShowAlertsIntentService() {
        super("ShowAlertsIntentService");

    }




    @Override
    protected void onHandleIntent(Intent intent) {

        Log.e(TAG, "inside onHandleIntent of ShowAlertsIntentService");

        rrOnCallApp = (RROnCallApplication) getApplication();

        from = new DateTime();
        to = from.plusDays(1);

        DateTimeFormatter fmt = DateTimeFormat.forPattern("d-MMM-Y");
        String formattedfrom = fmt.print(from);
        String formattedTo = fmt.print(to);

        alertsArray = rrOnCallApp.webService.getAlerts("1", formattedfrom, formattedTo);


        if(alertsArray != null){

            Log.e(TAG, "size of alertArray = " + alertsArray.size());



            String noCallsStatus = null;
            String outOfRangeStatus = null;

            ArrayList arrayList = (ArrayList) alertsArray.get(0);
            noCallsStatus = (String) arrayList.get(0);















                if((alertsArray.size() == 1) &&  (noCallsStatus.equalsIgnoreCase("no sig") ||  noCallsStatus.equalsIgnoreCase("no data"))){

                    //do nothing
                    Log.e(TAG, "no data or no sig sent back when getting alerts");

                }else{



                        Intent intentShowAlertsActivity = new Intent(this, ShowAlertsActivity.class);
                        Bundle b = new Bundle();
                        b.putSerializable("alertsarray", alertsArray);
                        intentShowAlertsActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intentShowAlertsActivity.putExtra("alertsarraybundle", b);
                        startActivity(intentShowAlertsActivity);

                        }

            }else{

                Log.e(TAG, "alertsArray = null!!!!!");

            }



    }

}

.

public class AlertsFragment extends ListFragment{

     private static final String TAG = AlertsFragment.class.getSimpleName();
     MySimpleArrayAdapter myAdapter;
     ArrayList<String> alertsArray;
     TextView alertTitle;


     @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            Bundle b = getArguments();

            if(b != null){

            alertsArray = (ArrayList<String>) b.get("alertsArray");

            Log.e(TAG, "alertsArray in AlertsFragment has size " + alertsArray.size());



            }else{

                Log.e(TAG, "Bundle b = null!!!!!!!!!!!");

            }




        }//end of onCreate

. [Edit1]

07-07 15:46:26.056: E/AndroidRuntime(8253): FATAL EXCEPTION: IntentService[ShowAlertsIntentService]
07-07 15:46:26.056: E/AndroidRuntime(8253): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
07-07 15:46:26.056: E/AndroidRuntime(8253):     at android.app.ContextImpl.startActivity(ContextImpl.java:864)
07-07 15:46:26.056: E/AndroidRuntime(8253):     at android.content.ContextWrapper.startActivity(ContextWrapper.java:276)
07-07 15:46:26.056: E/AndroidRuntime(8253):     at com.carefreegroup.rr3.carefreeoncall.ShowAlertsIntentService.onHandleIntent(ShowAlertsIntentService.java:90)
07-07 15:46:26.056: E/AndroidRuntime(8253):     at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
07-07 15:46:26.056: E/AndroidRuntime(8253):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-07 15:46:26.056: E/AndroidRuntime(8253):     at android.os.Looper.loop(Looper.java:137)
07-07 15:46:26.056: E/AndroidRuntime(8253):     at android.os.HandlerThread.run(HandlerThread.java:60)

.

[edit 2]

<activity
            android:name=".ShowAlertsActivity"
            android:screenOrientation="landscape" />

edit3

@Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        Log.e(TAG, "Inside onNewIntent");


        alertsArray = (ArrayList<String>) getIntent().getBundleExtra("alertsarraybundle").get("alertsarray");

        Log.e(TAG, "size of alertArray in ShowAlertsActivity = " + alertsArray.size());

        Bundle b = new Bundle();
        b.putSerializable("alertsArray", alertsArray);


        Fragment newFragment = new AlertsFragment();
        newFragment.setArguments(b);
        FragmentTransaction transaction = getFragmentManager().beginTransaction();


        // Replace whatever is in the fragment_container view with this fragment,
        // and add the transaction to the back stack
        transaction.replace(R.id.showalertslistfragment_container, newFragment);
        //transaction.addToBackStack(null);

        // Commit the transaction
        transaction.commit();


    }
turtleboy
  • 8,210
  • 27
  • 100
  • 199

1 Answers1

1

Use android:launchMode="singleInstance" for the activity in the Android Manifest. This'll ensure that only a single instance of the activity is maintained and the user won't have to click 12 times.

Check this answer for more details :

Android singleTask or singleInstance launch mode?

Edit 1 : If your activity is already running, you'll receive the new intent in onNewIntent() method. You can refresh your list there.

Check out this answer : https://stackoverflow.com/a/5810336/1239966

Community
  • 1
  • 1
Shivam Verma
  • 7,973
  • 3
  • 26
  • 34
  • I've commented out the FLAG_ACTIVITY_NEW_TASK in the service. I'm using android:launchMode="singleInstance" for the actrivity in the manifest and i have ovewrridden the onNewIntent in the activity. I'm still getting the error in edit1 – turtleboy Jul 07 '14 at 15:09
  • There's completely different method which'll work for sure but before that, try using `Intent.FLAG_FROM_BACKGROUND ` in place of `Intent.FLAG_ACTIVITY_NEW_TASK);` – Shivam Verma Jul 07 '14 at 15:20
  • My mistake it works now. I thought onNewIntent was never executing, but i wasn't waiting till al tleast one instance of my activity was already on the stack. It's me being an idiot! Thanks alot. – turtleboy Jul 08 '14 at 09:34
  • I used the Intent.FLAG_ACTIVITY_NEW_TASK in conjunction with android:launchMode="singleInstance" and overriding onNewIntent in the Activity. – turtleboy Jul 08 '14 at 09:37