Marcel Adel
Marcel Adel

Reputation: 181

calling activity from a service class

i'm developing an app where i have an activity A when it's on pause state i'm calling a service class that creates a window when the window pressed i want to recall the activity A from the back stack again in the last pause state , i tried a lot of things but it keeps recreate the activity , i tried singleTask lunchMode and a lot of things but nothing works

here is my service class:

public class ChatHeadService extends Service {

   public static WindowManager windowManager;
   public static ImageView chatHead;
   public static View myview;

    @Override public IBinder onBind(Intent intent) {
        // Not used
        return null;
    }

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

       LayoutInflater li = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                //WindowManager.LayoutParams.TYPE_INPUT_METHOD |
                WindowManager.LayoutParams.TYPE_PHONE,// | WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
                WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
                PixelFormat.TRANSLUCENT);

        params.gravity = Gravity.RIGHT | Gravity.TOP;
        myview = li.inflate(R.layout.dial_actionbar, null);
        chatHead=(ImageView)myview.findViewById(R.id.dialbar);
        chatHead.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                windowManager.removeView(myview);
                Intent goback = new Intent(ChatHeadService.this, IncomingCallScreen.class);
                goback.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                ChatHeadService.this.startActivity(goback);
                return true;

            }
        });

        windowManager.addView(myview, params);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        windowManager.removeView(myview);
    }


}

and here is the part where i call the service: @Override

 protected void onPause() {
        super.onPause();
        if(incomingCall==true)
        {
            Intent gotoBackService=new Intent(getApplicationContext(),ChatHeadService.class);
            startService(gotoBackService);

        }

    }

Upvotes: 0

Views: 852

Answers (5)

Ganpat Kaliya
Ganpat Kaliya

Reputation: 898

try this,

The problem is you can't use startActivity method on a broadcast receiver try with this one.

Intent mIntent=new Intent(FinishService.this,Information.class);

sendBroadcast(mIntent);

Upvotes: 0

Marcel Adel
Marcel Adel

Reputation: 181

this what worked for me Intent in=new Intent().setClass(ChatHeadService.this,IncomingCallScreen.class); in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); in.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); ctx.startActivity(in);

and set launchMOde:SingleTop

Upvotes: 1

Ashish Shukla
Ashish Shukla

Reputation: 1047

Use this code and declare this

Context ctx=this;

Intent in=new Intent().setClass(MyAlarmService.this,Reminder.class);
in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   
in.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);   
ctx.startActivity(in);

also add android:launchMode="singleTop" in your manifest for this activty

Upvotes: 0

Nanoc
Nanoc

Reputation: 2381

If you use FLAG_ACTIVITY_NEW_TASK it will recreate it every time.

Try using FLAG_ACTIVITY_BROUGHT_TO_FRONT as explained here

If this not cause a conflict with your proyect needs you can set the launchmode in app manifiest.xml

android:launchMode="singleInstance"

reference:Resume started activity from Service

Hope this helps.

Upvotes: 0

Jas
Jas

Reputation: 3212

You can call the new activity like this.

 Intent intent = new Intent(getBaseContext(),Activity.class);
 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 getApplication().startActivity(intent);   

Upvotes: 0

Related Questions