jerry2007
jerry2007

Reputation: 21

Android: Orientation changes erase modifications made to my ImageView

I have app in which I have ImageView. I open new activity, where I paint something by finger and this bitmap return to my ImageView. everything is ok but when I change orientation now, my activity with ImageView is repaint or restart and imageview is empty. I try everything, I try SaveState and restore state, try configurationChange, and other, but nothing is working...

package jilova.Android.TextFolder;

import java.io.ByteArrayOutputStream;

import jilova.Android.Enums;
import jilova.Android.R;
import jilova.Android.DatabaseFolder.LocalDB;
import jilova.Android.DatabaseFolder.RequestRow;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.Toast;

public class Text extends Activity{

    private static EditText t1;
    private static EditText t2;
    private static EditText t3;
    private static EditText t4;
    private static ImageView iv1;
    private static Context c;
    private static Activity ac;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.text);

        Object o = this.getLastNonConfigurationInstance();
        if(o!=null)
        {
            Enums.sign=(Bitmap)o;
        }

        c=this.getApplicationContext();
        ac=this;

        ImageView iv = (ImageView)this.findViewById(R.id.imageView1);
        iv.setDrawingCacheEnabled(true);
        t1 = (EditText)this.findViewById(R.id.TEXTNote);
        t2 = (EditText)this.findViewById(R.id.TextET2);
        t3 = (EditText)this.findViewById(R.id.TextET3);
        t4 = (EditText)this.findViewById(R.id.TextET4);

        //iv.setImageBitmap(Bitmap.createScaledBitmap(Enums.sign, iv.getWidth(), iv.getHeight(), false).copy(Config.ARGB_8888, true));

    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        // Checks the orientation of the screen
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {

        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){

        }
        // Checks whether a hardware keyboard is available
        if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {

        } else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {

        }
    }





    public static RequestRow getData()
    {
        RequestRow row = new RequestRow();
        row.SetREQUESTNOTE(t1.getText().toString());
        row.SetREQUESTTYPEID(Integer.parseInt(t2.getTag().toString()));
        row.SetTYPTECHUDRZBYID(Integer.parseInt(t3.getTag().toString()));
        row.SetOBJECTID(Integer.parseInt(t4.getTag().toString()));
        return row;
    }

    public static void setData(String REQUESTTYPEID ,String TYPTECHUDRZBYID,String OBJECTID,String REQUESTNOTE,String REQUESTID)
    {
        t1.setText(REQUESTNOTE);
        t2.setTag(REQUESTTYPEID);
        t2.setText(LocalDB.dbGetRequestTypeByID(c, Integer.parseInt(REQUESTTYPEID)));
        t3.setTag(TYPTECHUDRZBYID);
        t3.setText(LocalDB.dbGetTypTechUdrzbyByID(c, Integer.parseInt(TYPTECHUDRZBYID)));
        t4.setTag(OBJECTID);
        t4.setText(LocalDB.dbGetObjectByID(c, Integer.parseInt(OBJECTID)));
        Bitmap b = LocalDB.dbGetDocumentByID(c, Integer.parseInt(REQUESTID));
        if(b!=null)
        {
            iv1.setImageBitmap(b);
        }
    }

    public static Bitmap getSign()
    {
        iv1.buildDrawingCache();
        Bitmap ret = iv1.getDrawingCache();

        if(Enums.EmptySignHash)
        {
            return null;
        }
        else
        {
            return ret;
        }
    }

    public void Sign(View button)
    {
        Intent s = new Intent(Text.this,Sign.class);
        startActivityForResult(s,Enums.SIGNREQUESTID);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if(resultCode!=Activity.RESULT_OK)
        {
            return;
        }
        Enums.IDLocal=-1;
        if(requestCode==Enums.GetData)
        {
            Bundle  extras = data.getExtras();
            if(extras !=null)
            {
                EditText et = (EditText)this.findViewById(extras.getInt("ViewID"));
                et.setText(extras.getString("Value"));
                et.setTag(extras.get("ID"));
            }
        }
        else if(requestCode==Enums.SIGNREQUESTID)
        {
            Bundle  extras = data.getExtras();
            if(extras !=null)
            {
                ImageView iv = (ImageView)this.findViewById(R.id.imageView1);
                try
                {
                    byte[] b = extras.getByteArray("Bitmap");
                    Enums.sign = Bitmap.createBitmap(BitmapFactory.decodeByteArray(b, 0, b.length), 0, 0, ac.getWindowManager().getDefaultDisplay().getWidth(), 200);
                iv.setImageBitmap(Bitmap.createScaledBitmap(Enums.sign, iv.getWidth(), iv.getHeight(), false));

                }
                catch(Exception e)
                {
                    int a=0;
                }
            }
            Enums.EmptySignHash=false;
        }
    }

    public static void DeleteAll()
    {
        EditText t = (EditText)ac.findViewById(R.id.TEXTNote);
        t.setText("");
        t.setTag(-1);
        t = (EditText)ac.findViewById(R.id.TextET2);
        t.setText("");
        t.setTag(-1);
        t = (EditText)ac.findViewById(R.id.TextET3);
        t.setText("");
        t.setTag(-1);
        t = (EditText)ac.findViewById(R.id.TextET4);
        t.setText("");
        t.setTag(-1);

        ImageView iv = (ImageView)ac.findViewById(R.id.imageView1);
        //Bitmap b = Bitmap.createBitmap(Enums.createColors(), 0, Enums.STRIDE, Enums.WIDTH, Enums.HEIGHT, Bitmap.Config.ARGB_8888).copy(Bitmap.Config.ARGB_8888, true);
        iv.setImageBitmap(Bitmap.createScaledBitmap(Enums.sign, ac.getWindowManager().getDefaultDisplay().getWidth(), iv.getLayoutParams().height, false));

        Enums.EmptySignHash=true;
    }

    public void Delete(View button)
    {
        EditText t = (EditText)this.findViewById(R.id.TEXTNote);
        t.setText("");
        t.setTag(-1);
        t = (EditText)this.findViewById(R.id.TextET2);
        t.setText("");
        t.setTag(-1);
        t = (EditText)this.findViewById(R.id.TextET3);
        t.setText("");
        t.setTag(-1);
        t = (EditText)this.findViewById(R.id.TextET4);
        t.setText("");
        t.setTag(-1);

        ImageView iv = (ImageView)this.findViewById(R.id.imageView1);
        //Bitmap b = Bitmap.createBitmap(Enums.createColors(), 0, Enums.STRIDE, Enums.WIDTH, Enums.HEIGHT, Bitmap.Config.ARGB_8888).copy(Bitmap.Config.ARGB_8888, true);
        iv.setImageBitmap(Bitmap.createScaledBitmap(Enums.sign, ac.getWindowManager().getDefaultDisplay().getWidth(), iv.getLayoutParams().height, false));

        Enums.EmptySignHash=true;

    }

    public void GetData(View button)
    {
        if(button.getId()==R.id.TextIB3)
        {
            Intent ChooseAction = new Intent(Text.this,ChooseData.class);
            Enums.Data = LocalDB.dbLocalSelect(this.getApplicationContext(),"Select typtechudrzbyid,typtechudrzbydesc from typtechudrzby");
            if(Enums.Data==null)
            {
                Toast t=Toast.makeText(this, "Chyba lokalni db", Toast.LENGTH_SHORT);
                t.setGravity(Gravity.CENTER, 0, 0);
                t.show();
                return;
            }
            ChooseAction.putExtra("ViewID", R.id.TextET3);
            startActivityForResult(ChooseAction,Enums.GetData);
        }
        else if(button.getId()==R.id.TextIB2)
        {
            Intent ChooseAction = new Intent(Text.this,ChooseData.class);
            Enums.Data = LocalDB.dbLocalSelect(this.getApplicationContext(),"Select Requesttypeid,requestname from requesttype");
            if(Enums.Data==null)
            {
                Toast t=Toast.makeText(this, "Chyba lokalni db", Toast.LENGTH_SHORT);
                t.setGravity(Gravity.CENTER, 0, 0);
                t.show();
                return;
            }
            ChooseAction.putExtra("ViewID", R.id.TextET2);
            startActivityForResult(ChooseAction,Enums.GetData);
        }
        else if(button.getId()==R.id.TextIB4)
        {
            Intent ChooseAction = new Intent(Text.this,TreeData.class);
            Enums.TreeData = LocalDB.dbLocalSelectTree(this.getApplicationContext(),"Select objectid,objectname,objectref from Objects");
            if(Enums.TreeData==null)
            {
                Toast t=Toast.makeText(this, "Chyba lokalni db", Toast.LENGTH_SHORT);
                t.setGravity(Gravity.CENTER, 0, 0);
                t.show();
                return;
            }
            ChooseAction.putExtra("ViewID", R.id.TextET4);
            startActivityForResult(ChooseAction,Enums.GetData);
        }
        else
        {
            //throw new Exception();
        }       

    }
}

Upvotes: 2

Views: 2931

Answers (3)

backslashN
backslashN

Reputation: 2875

When the configuration is changed, the whole view is re-created. So, we need to retain the imageView resource. The best way to do this is to handle the orientation change by creating a Retained fragment.

You can find the perfect documentation on Android developer's page.

P.S.: Adding android:configChanges="keyboardHidden|orientation" to the manifest is highly not recommended.

Upvotes: 0

Flo
Flo

Reputation: 27455

I think you forgot to override the onRetainNonConfigurationInstance() method where you return your bitmap so it will be passed to the new activity. In the new activity you can retrieve the bitmap as you already do by calling getLastNonConfigurationInstance().

Upvotes: 0

Joseph Earl
Joseph Earl

Reputation: 23432

When a configuration change such as a screen rotation occurs by default your Activity is destroyed and then recreated (onDestroy of the current activity is called, and then the onCreate of a new version of your activity is called).

You can either:

  • Stop Android recreating your activity when a configuration change occurs. To do this add android:configChanges="keyboardHidden|orientation" to the activity tag in your manifest. This is not recommend since if you want a different layout etc for different configurations you will have to handle changing the layout yourself.
  • Override onRetainNonConfigurationInstance and return your bitmap from that. In onCreate check if the last non-configuration instance is not null, in which case cast it to a bitmap and then set the image.

For the latter case, use something like the following:

@Override
public void onCreate(Bundle savedInstanceState) {
    ...

    // Check if our activity was just destroyed and re-created
    final Object retainedFromConfigChange = getLastNonConfigurationInstance();
    if (retainedFromConfigChange != null) {
        // Activity has just been recreated, get the image we were working on
        // before the configuration change
        ImageView iv = (ImageView)ac.findViewById(R.id.imageView1);
        iv.setImageBitmap((Bitmap) retainedFromConfigChange);
    }

    ...
}

@Override
public Object getLastNonConfigurationInstance() {
    ImageView iv = (ImageView)ac.findViewById(R.id.imageView1);
    // We have to return a plain old Bitmap and not a drawable of any sorts
    // or we will get memory leaks so we need to extract the bitmap from the drawable
    return ((BitmapDrawable) iv.getDrawable()).getBitmap();
}

Upvotes: 1

Related Questions