Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6723921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:37:37+00:00 2026-05-26T09:37:37+00:00

I have onUpdate(…) metod of MyAppWidgetProvider class with the code: … Intent launchAppIntent =

  • 0

I have onUpdate(…) metod of MyAppWidgetProvider class with the code:

...
Intent launchAppIntent = new Intent(context, SearchActivity.class);        
PendingIntent launchAppPendingIntent = PendingIntent.getActivity(context, 0, launchAppIntent, 0);        
remoteView.setOnClickPendingIntent(R.id.logo, launchAppPendingIntent);  
...

It works fine. New SearchActivity starts by clicking on widget. Now, I want to start on click not the SearchActivity, but an activity from the top of the stack of my task. For this purpose I change above code to:

ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(30);        
for (ActivityManager.RunningTaskInfo task:tasks) {
    if (task.baseActivity.getShortClassName().equals(".MainActivity")) {    
        try {
            Intent launchAppIntent = new Intent(context, Class.forName(task.topActivity.getClassName()));
            PendingIntent launchAppPendingIntent = PendingIntent.getActivity(context, 0, launchAppIntent, 0);        
            remoteView.setOnClickPendingIntent(R.id.logo, launchAppPendingIntent);                     
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(CashWidgetProvider.class.getName()).log(Level.SEVERE, null, ex);
        }                  
    }
}

that doesn’t work. Foreground activity in task stack doesn’t appear on monitor. Moreover, I don’t get any messages in log running this code.

However, the same code works fine being placed in some method of the class, inherited from Activity.

ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(30);        
for (ActivityManager.RunningTaskInfo task:tasks) {
    System.out.println(task.baseActivity.getShortClassName());
    if (task.baseActivity.getShortClassName().equals(".MainActivity")) {
        try {
            Intent launchAppIntent = new Intent(this, Class.forName(task.topActivity.getClassName())); 
            startActivity(launchAppIntent);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(AbstractActivity.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
} 

How can I get it working for AppWidget!?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T09:37:37+00:00Added an answer on May 26, 2026 at 9:37 am

    The problem appears due to misunderstanding of widget lifecycle, as usual… To get it working I have to create MyService class inherited from Service overriding onStart method with code:

    ...
    Context context = this.getApplicationContext();
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(30);        
    for (ActivityManager.RunningTaskInfo task:tasks) {
        if (task.baseActivity.getShortClassName().equals(".MainActivity")) {    
            try {
                Intent launchTopIntent = new Intent(context, Class.forName(task.topActivity.getClassName()));
                    context.startActivity(launchTopIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));             
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(CashWidgetProvider.class.getName()).log(Level.SEVERE, null, ex);
            }                  
        }
    } 
    ...
    

    and call above MyService from AppWidgetProvider class by means of the code:

    Intent launchAppIntent = new Intent(context, MyService.class);
    launchAppIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
    PendingIntent launchAppPendingIntent = PendingIntent.getService(
                context.getApplicationContext(), 0, launchAppIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    remoteView.setOnClickPendingIntent(R.id.logo, launchAppPendingIntent2); 
    

    Don’t forget to add this new service in AndroidManifest.xml under section

    <application>
        ...
        <service android:name=".MyService"/>
    </application>
    

    P.S. Probably, it would be better to use BroadcastReceiver instead of Service.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code in my AppWidgetProvider class. @Override public void onUpdate(Context context,
I have the following code: public class SomeClass { //InterfaceUpdateListener is an interface private
I have problem with session and query execution, please see code below. class A
I have problem with session and query execution,please see code below. class A implements
Have you guys had any experiences (positive or negative) by placing your source code/solution
I have the following code: MSG mssg; // run till completed while (true) {
I have the extremely simple AppWidgetProvider for a test widget: public class Test extends
I have enabled sfDoctrineGuardPlugin, so I have change myUser class to my class extends
I have a base class Foo that has an Update() function, which I want
I have an OnIdle handler in my D2006 app. With this code: procedure TMainForm.ApplicationEvents1Idle(Sender:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.