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

  • Home
  • SEARCH
  • 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 558607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:09:00+00:00 2026-05-13T12:09:00+00:00

I have a widget that displays an analog clock. What I would like is

  • 0

I have a widget that displays an analog clock. What I would like is for the the widget to write to a database the time when a user clicks on the widget. I’ve already got the databaseHelper class and have an Activity that displays a screen showing the current date and time and writes the time to a database.

I followed the tutorial here: Analog Clock Tutorial and ended up with this:

public void onReceive(Context context, Intent intent)
{
    String action = intent.getAction();
    if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action))
    {
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
        this.mIntent = new Intent(context, AskTheTime.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, mIntent, 0);
        views.setOnClickPendingIntent(R.id.Widget, pendingIntent);

        AppWidgetManager.getInstance(context).updateAppWidget(intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS), views);
    }
}

The AskTheTime class extends activity and logs to the database in onCreate(). But this means that it displays only the time when the widget was started – not when it was clicked. (I guess this is because I’m starting a pendingIntent) I’m not sure If I should put the database write in another method or if I should be using Intents to do this sort of thing from widgets. Any pointers would be great! I’ve look at several tutorials for intents but none of them seem very clear to me.

  • 1 1 Answer
  • 3 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-13T12:09:01+00:00Added an answer on May 13, 2026 at 12:09 pm

    When your widget is pressed, the OS activates the pending intent you set on the widget.

    It looks like you are opening an activity AskTheTime when the widget is pressed.

    So the problem is that AskTheTime might have already been created when the widget is pressed, so the onCreate() isn’t called again.

    What you could do is try doing your logging in onStart() or onResume() instead of, or in addition to, onCreate() inside your activity.

    From the documentation regarding onStart:

    Called when the activity is becoming
    visible to the user.

    And onResume:

    Called when the activity will start
    interacting with the user. At this
    point your activity is at the top of
    the activity stack, with user input
    going to it.

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

Sidebar

Related Questions

I have a widget that displays a set of information. What I would like
I have a selectmultiple choice widget that gets data from mysql database and displays
Let's say that I have a widget that displays summary information about how many
So I have a JS function that displays a widget at the bottom of
I have a widget that displays the picture of some of my contacts and
I have a WPF 'monitor widget' that displays CPU, RAM and disk performance as
I have written a small WPF widget using C# that displays the current CPU
I have a widget that displays a filesystem hierarchy for convenient browsing (basically a
I have an autocomplete widget that displays text w/ images. Currently my results look
I have this widget in android, that displays an image and gets updated periodically

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.