I am working on home screen widget, and I am facing problems when
changing orientation of the phone - widget stops responding to clicks.
I tried to search through groups to find solution, and closest topic
was one here:
http://groups.google.com/group/android-developers/browse_thread/thread/9c39f2279f8cdac3/
However, I was unable to find solution of my problem there. So, here
are details of my problem:

AppWidget has two ImageViews, used to navigate through
messages(forward and back). Messages are shown in TextView within a
widget. When I add widget to the home screen, it works perfectly -
both buttons, left and right, work ok, messages are changed when I
click on buttons.

Once I switch orientation of the phone, buttons are not responding
anymore! So I tried to eliminate piece by piece of code, and came to
strange situation:when one button is used to change message,
everything works fine. But, if other button is also used to change
message, application stops working after orientation change.

Simply - in code provided below, when I comment out part of the code
which handles ACTION_RIGTH in function onReceive, application is
working properly when orientation is changed. If I uncommend it,
application stops working on orientation change. Can anyone tell me
what I am doing wrong here? Content of all project files is given
bellow. Thanks!

----------------------------------------------------
File - AndroidManifest:
----------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android";
      package="com.code_rs.android"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/
app_name">
                        <!-- Broadcast Receiver that will process AppWidget 
updates -->
                        <receiver android:name=".NavigationWidget" 
android:label="@string/
app_name">
                                <intent-filter>
                                        <action 
android:name="android.appwidget.action.APPWIDGET_UPDATE" /
>
                                        <action
android:name="com.code_rs.android.NavigationWidget.ACTION_LEFT"/>
                                        <action
android:name="com.code_rs.android.NavigationWidget.ACTION_RIGTH"/>
                                </intent-filter>
                                <meta-data 
android:name="android.appwidget.provider"
                                android:resource="@xml/widget_ots" />
                        </receiver>
    </application>
    <uses-sdk android:minSdkVersion="4" />

</manifest>
----------------------------------------------------
File - main.xml:
----------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android";
  android:id="@+id/rel_lay"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="@drawable/widget_frame_portrait2x2">
  <ImageView
        android:id="@+id/btn_prev"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="6dip"
        android:layout_alignParentTop="false"
        android:layout_alignParentBottom="true"
        android:src="@drawable/back" android:layout_marginLeft="16dip"
android:layout_marginBottom="12dip"/>
  <ImageView
        android:id="@+id/btn_next"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="false"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="false"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:src="@drawable/next"
android:layout_marginBottom="12dip" android:layout_marginRight="16dip"/
>
    <TextView
        android:id="@+id/sms_text"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/btn_prev"
        android:text="Message text goes here"
android:layout_marginLeft="16dip" android:layout_marginRight="16dip"
android:paddingTop="10dip" android:textColor="#000000"
android:textSize="12dip" android:layout_marginTop="30dip"/>
</RelativeLayout>
----------------------------------------------------
File - widget_ots.xml:
----------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/
android"
    android:minWidth="146dip"
    android:minHeight="146dip"
    android:updatePeriodMillis="21600000"
    android:initialLayout="@layout/main" />
----------------------------------------------------
File - NavigationWidget.java:
----------------------------------------------------
package com.code_rs.android;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;

public class NavigationWidget extends AppWidgetProvider {
        public static final String TAG = "NavigationWidget";
        private static final String ACTION_LEFT =
"com.code_rs.android.NavigationWidget.ACTION_LEFT";
        private static final String ACTION_RIGTH =
"com.code_rs.android.NavigationWidget.ACTION_RIGTH";

        private String[] testMsgs = {"First","Second, quite long and should
wrap around to at least two lines","Third","Fourth ...","And final
fifth"};
        private static int indx = 0;

    @Override
    public void onUpdate(Context context, AppWidgetManager
appWidgetManager, int[] appWidgetIds) {
        Log.d(TAG, "onUpdate(): ");
        for (int widgetId:appWidgetIds) {
                RemoteViews remoteView = new
RemoteViews(context.getPackageName(), R.layout.main);
                attachIntents(context,remoteView,widgetId);
        }
    }
    protected void attachIntents(Context context, RemoteViews
remoteView, int appWidgetId) {
        /* Setup left button */
        Log.d(TAG,"Widget:"+appWidgetId);
        Intent left = new Intent(context, NavigationWidget.class);
        left.setAction(ACTION_LEFT);
        left.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
appWidgetId);
        PendingIntent leftPendingIntent =
PendingIntent.getBroadcast(context, 0, left, 0);
        remoteView.setOnClickPendingIntent(R.id.btn_prev,
leftPendingIntent);
        /* Setup right button  */
        Intent right = new Intent(context, NavigationWidget.class);
        right.setAction(ACTION_RIGTH);
        right.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
appWidgetId);
        right.putExtra("msg", "Message for Button right");
        PendingIntent rightPendingIntent =
PendingIntent.getBroadcast(context, 0, right, 0);
        remoteView.setOnClickPendingIntent(R.id.btn_next,
rightPendingIntent);
 
AppWidgetManager.getInstance(context).updateAppWidget(appWidgetId,
remoteView);
    }
    @Override
    public void onReceive(Context context, Intent intent) {

        final String action = intent.getAction();
        Log.d(TAG, "OnReceive:Action: " + action);
        if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action))
{
            final int appWidgetId =
intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
            if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID)
{
                this.onDeleted(context, new int[] { appWidgetId });
            }
        }
        else if (ACTION_LEFT.equals(action)) {
                        indx--;
                        if (indx<0) {
                                indx = testMsgs.length-1;
                        }
                        String msg = testMsgs[indx];
                        Log.d(TAG, msg);
                        RemoteViews remoteView = new 
RemoteViews(context.getPackageName(),
R.layout.main);
                        remoteView.setTextViewText(R.id.sms_text, msg);
                        ComponentName thisWidget = new ComponentName(context,
NavigationWidget.class);
                        AppWidgetManager manager = 
AppWidgetManager.getInstance(context);
                        manager.updateAppWidget(thisWidget, remoteView);
            final int appWidgetId =
intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
                Log.d(TAG, "Apt widget id="+appWidgetId);
            if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID)
{
                attachIntents(context,remoteView,appWidgetId);
            }
        } else if (ACTION_RIGTH.equals(action)) {
                        indx++;
                        if (indx==testMsgs.length) {
                                indx = 0;
                        }
                        /*
                         * !!!!!!!!!! Problem is here! If we remove comment 
from following
section,
                         * !!!!!!!!!! widget stops responding after orientation 
switch
                        */
                        /*
                        String msg = testMsgs[indx];
                        Log.d(TAG, msg);
                        RemoteViews remoteView = new 
RemoteViews(context.getPackageName(),
R.layout.main);
                        remoteView.setTextViewText(R.id.sms_text, msg);
                        ComponentName thisWidget = new ComponentName(context,
NavigationWidget.class);
                        AppWidgetManager manager = 
AppWidgetManager.getInstance(context);
                        manager.updateAppWidget(thisWidget, remoteView);
            final int appWidgetId =
intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
                Log.d(TAG, "Apt widget id="+appWidgetId);
            if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID)
{
                attachIntents(context,remoteView,appWidgetId);
            }
            */
        }
        super.onReceive(context, intent);
    }
}

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to