Windows Floating open different activities without stopping the previous one

0

I'm creating an APP that is mainly used from a Windows Float style that uses Facebook Messenger, by clicking on it opens several options (buttons)

For example, the first opens the Google Maps application, the second an Internal Activity, etc ...

What happens is that if I open Google Maps from Windows Float, and from the Windows Float I open an activity and click again on the Google Maps icon, it opens from scratch as new, losing the destination or what is I had. And if I go back to the CallsActivity it happens the same thing has stopped the activity to change losing what I had.

        ImageView gpsButton = (ImageView) mFloatingView.findViewById(R.id.btn_GPS);
    gpsButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(Service_SoftKeysFloat.this, "GPS.", Toast.LENGTH_LONG).show();
            // mode:
            //      d -> Auto
            //      w -> a pie
            //      d -> en bicicleta
            // avoid
            //      t -> evitar peajes
            //      h -> evitar autopistas
            //      f -> evitar transbordadores
            // San Martin Coordenadas: 40.389932, -4.372887

            //Uri gmmIntentUri = Uri.parse("google.navigation:q=Calle Bureba 26,Leganes&mode=d&avoid=tf");
            //Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
            IntentGPS = new Intent(Intent.ACTION_VIEW);
            IntentGPS.setPackage("com.google.android.apps.maps");
            if (IntentGPS.resolveActivity(getPackageManager()) != null) {
                startActivity(IntentGPS.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY));
            }

            // Cierra la lista de opciones
            expandedView.setVisibility(View.GONE);
            collapsedView.setVisibility(View.VISIBLE);
        }
    });

    //Set the Call button.
    ImageView callButton = (ImageView) mFloatingView.findViewById(R.id.btn_Calls);
    callButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(Service_SoftKeysFloat.this, "Calls.", Toast.LENGTH_LONG).show();
            IntentCalls = new Intent(getApplicationContext(), CallsActivity.class);
            startActivity(IntentCalls.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY));

            // Cierra la lista de opciones
            expandedView.setVisibility(View.GONE);
            collapsedView.setVisibility(View.VISIBLE);
        }
    });
    
asked by Javier Martínez Ramírez 03.05.2018 в 17:33
source

1 answer

0

What happens is that every time you click on your "Buttons" you recreate a new activity. If you want to summarize the activities once started, you should use:

startActivityIfNeeded the parameters they receive are the following, according to the official documentation :

  

Parameters

     

intent: is of type Intent describes the intent or activity to throw This value should never be null.

     

requestCode: is a int and if the value is > = 0, this code will be returned in the result when the activity ends, as   Described in startActivityForResult (Intent, int)

     

options: is a Bundle , that is, additional points on how the activity should start. This value can be null.

In your case you should start Activitys in the following way:

startActivityIfNeeded(IntentGPS.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT), 0);

startActivityIfNeeded(IntentCalls.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT), 0);

As its name says, it will only trigger the creation of the Activity ( onCreate ) when required (if it is not created). Otherwise, if it is already started, it will trigger the other methods of its life cycle. The Intent.FLAG_ACTIVITY_REORDER_TO_FRONT flag is for reordering that Activity in front of the Stack.

Note: Keep in mind that if you are going to use this way of calling Activitys, you should manage their life cycles with the listeners that affect the mobile hardware. For example if on a screen you use the PowerManager or the proximity sensor, to turn the screen on and off or to know if an object is near or far away. If you do not remove the subscription to those listeners in a non-active Activity. These listeners will be constantly firing until the Activity that manages them is destroyed. The recommendation is that in OnPause/OnResume you make the set of the listeners correctly.

    
answered by 03.05.2018 / 19:12
source