3 Aug 2017

Activity LifeCycle onCreate beating screen orientation change

package com.ani;

import android.app.*;import android.os.*;import android.content.*;import android.app.DatePickerDialog.*;import android.app.TimePickerDialog.*;
import android.view.View.*;import android.view.*;import android.widget.*;import android.database.sqlite.*;import android.database.*;
import java.text.*;import java.util.*;import android.widget.*;import android.graphics.Color;

public class HomePage extends Activity

{
SQLiteDatabase db;LinearLayout ll;Context ctx;
private static String DB="TEST.db",TBL="MY_TABLE";
private static boolean screenChange=false;

@Override
protected void onSaveInstanceState(Bundle b)
{
    super.onSaveInstanceState(b);
String str="Screen Change="+String.valueOf(screenChange)+"....";
    Toast.makeText(ctx,str+"You are changing orientation...",Toast.LENGTH_SHORT).show();
screenChange=true;
   
}

@Override
public void onCreate(Bundle b)
        {
super.onCreate(b);
ctx=getApplicationContext();
if(!screenChange)
{
String str="Screen Change="+String.valueOf(screenChange);

Toast.makeText(ctx,str+"Trying to Drop Previous & Create New Table...",Toast.LENGTH_SHORT).show();

killTable();makeTable();str=" Screen Change="+String.valueOf(screenChange)+"....";
Toast.makeText(ctx,str,Toast.LENGTH_SHORT).show();

showData();

Toast.makeText(ctx,str+"Trying to Put Data in Table...",Toast.LENGTH_SHORT).show();
putData();

Toast.makeText(ctx,str+"Trying to Show Data in Table...",Toast.LENGTH_SHORT).show();
showData();
}
}

public void makeTable()
{
try {
db=openOrCreateDatabase(DB,Context.MODE_PRIVATE,null);
db.execSQL(  "CREATE TABLE IF NOT EXISTS " + TBL +" (ID INTEGER PRIMARY KEY ,NAME TEXT,PLACE TEXT);"  );
db.close();
Toast.makeText(ctx,"Created Table successfully...",Toast.LENGTH_SHORT).show();
   }
catch(Exception e)
   {
Toast.makeText(ctx,"Error in Creating Table...",Toast.LENGTH_SHORT).show();
   }

}

public void killTable()
{
try {
db=openOrCreateDatabase(DB,Context.MODE_PRIVATE,null);
db.execSQL(  "DROP TABLE " + TBL );
db.close();
Toast.makeText(ctx,"Killed Table...",Toast.LENGTH_LONG).show();
   }
catch(Exception e)
   {
Toast.makeText(ctx,"Error encountered while Dropping Table...",Toast.LENGTH_LONG).show();
   }

}
public void putData()
{
try {
db=openOrCreateDatabase(DB,Context.MODE_PRIVATE,null);
db.execSQL(  "INSERT INTO " + TBL +" (NAME,PLACE) VALUES ('ANIMESH','KOLKATA')"  );
db.close();
Toast.makeText(ctx,"Successfully Inserted Records in Table...",Toast.LENGTH_SHORT).show();
   }
catch(Exception e)
   {
Toast.makeText(ctx,"Error in Inserting Records in Table...",Toast.LENGTH_SHORT).show();
   }

}
public void showData()
{
try {
db=openOrCreateDatabase(DB,Context.MODE_PRIVATE,null);
String qry="SELECT * FROM "+TBL;
Cursor allrows=db.rawQuery(qry,null);

if(allrows.moveToFirst())
{
do
 {
String id=allrows.getString(0),name=allrows.getString(1),place=allrows.getString(2);
String msg="Record No." + id + "  ->   Name: " + name + " , Place: " + place;
Toast.makeText(ctx,msg,Toast.LENGTH_SHORT).show();

 }
while(allrows.moveToNext());




}
else
{
Toast.makeText(ctx,"No Records as yet...",Toast.LENGTH_SHORT).show();
}


db.close();
Toast.makeText(ctx,"Successfully shown Records in Table...",Toast.LENGTH_SHORT).show();
   }
catch(Exception e)
   {
Toast.makeText(ctx,"Error in Retrieving Records in Table...",Toast.LENGTH_SHORT).show();
   }

}

1 comment: