Hi,

I am developing a simple two view app. I am overriding the
onKeyPressed to make the activity go into background when back key is
pressed, instead of destroying it. Now when I am on first view and I
sent the activity to background(using back key) , on again launching
the app, the first view appears correctly, but when I am on second
view and I send the activity to background, When I launch the app a
Black Screen Appears. I am not able to find a solution to it. Please
help. The relevant source code is:

1. main.xml :

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/
android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <RelativeLayout
        android:id="@+id/firstview"
        android:layout_height = "fill_parent"
        android:layout_width = "fill_parent"
        android:visibility="visible"
    >

        <TextView
                android:id="@+id/firstviewet"
                android:layout_width="fill_parent"
                android:layout_height = "60px"
                android:layout_alignParentTop="true"
                android:layout_alignParentLeft="true"
                android:layout_marginTop="20px"
                android:text="You are in first view"
        />

        <Button
                android:id="@+id/firstviewbutton"
                android:layout_width="fill_parent"
                android:layout_height = "wrap_content"
                android:layout_below="@id/firstviewet"
                android:layout_marginTop="20px"
                android:text="Press Me"
                android:layout_alignParentLeft="true"
        />

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/secondview"
        android:layout_height = "fill_parent"
        android:layout_width = "fill_parent"
        android:visibility="invisible"
    >

        <TextView
                android:id="@+id/secondviewet"
                android:layout_width="fill_parent"
                android:layout_height = "60px"
                android:layout_alignParentTop="true"
                android:layout_alignParentLeft="true"
                android:text="You are in second view"
        />

        <Button
                android:id="@+id/secondviewbutton"
                android:layout_width="fill_parent"
                android:layout_height = "wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true"
        />

    </RelativeLayout>

</FrameLayout>


2. BackgroundActivity.java:

package com.octro.demo.backgroundapp;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class BackgroundActivity extends Activity {

        AppState currentState;
        BackgroundAppHelper bgdAppHelper = null;

        View firstView, secondView;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        bgdAppHelper = BackgroundAppHelper.getInstance();



        //firstView = findViewById(R.id.firstview);
        //secondView = findViewById(R.id.secondview);

        if(bgdAppHelper.getCurrentState() == null)
                bgdAppHelper.startBgdActivityHandling(this);
        else
                bgdAppHelper.setAppState();

    }

    public void displayMainView()
    {
        setContentView(R.layout.main);
    }

    public View getFirstView()
    {
        if(firstView == null)
                firstView = findViewById(R.id.firstview);

        return firstView;
    }

    public View getSecondView()
    {
        if(secondView == null)
                secondView = findViewById(R.id.secondview);

        return secondView;
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if(keyCode == KeyEvent.KEYCODE_BACK)
        {
                if(bgdAppHelper.backKeyPressed())
                        return true;
        }
        return super.onKeyDown(keyCode, event);
   }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        Log.v("onStop", "in on stop method");
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        Log.v("onDestroy", "in on destroy method");
        super.onDestroy();
    }

    public boolean backKeyPressed()
    {
        moveTaskToBack(true);
                return true;
    }
}


3. BackgroundAppHelper.java :

package com.octro.demo.backgroundapp;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class BackgroundAppHelper
{
        private AppState currentState;
        private BackgroundActivity backgroundActivityObj = null;
        private View firstView = null;
        private View secondView = null;
        private Button button = null;
        private static BackgroundAppHelper backgroundAppHelper = null;

        private BackgroundAppHelper()
        {

        }

        public static BackgroundAppHelper getInstance()
        {
                if(backgroundAppHelper == null)
                        backgroundAppHelper = new BackgroundAppHelper();

                return backgroundAppHelper;
        }

        public void startBgdActivityHandling(BackgroundActivity bgAObj)
        {
                if(backgroundActivityObj == null)
                        backgroundActivityObj = bgAObj;

                startFirstView();
        }

        private void startFirstView()
        {

                backgroundActivityObj.displayMainView();

                if(firstView == null)
                        firstView = backgroundActivityObj.getFirstView();


                if(currentState == null)
                        currentState = new AppState();
                currentState.appState = EAppState.FirstView;

                button = (Button)firstView.findViewById(R.id.firstviewbutton);

                button.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                                // TODO Auto-generated method stub
                                startSecondView();
                        }
                });
        }

        public void setAppState()
        {
                if(currentState.appState == EAppState.SecondView)
                {
                        firstView.setVisibility(View.GONE);
                        secondView.setVisibility(View.VISIBLE);
                        backgroundActivityObj.displayMainView();
                }
        }

        public boolean backKeyPressed()
        {
                 return backgroundActivityObj.backKeyPressed();
        }

        private void startSecondView()
        {
                if(secondView == null)
                        secondView = backgroundActivityObj.getSecondView();

                firstView.setVisibility(View.GONE);
                secondView.setVisibility(View.VISIBLE);

                currentState.appState = EAppState.SecondView;
        }

        public AppState getCurrentState()
        {
                return currentState;
        }


}

4. AppState.java

package com.octro.demo.backgroundapp;

public class AppState
{
        EAppState appState;
}


5. EAppState.java

package com.octro.demo.backgroundapp;

public enum EAppState
{
        FirstView(0),
        SecondView(1);

        int eAppState;

        EAppState(int value)
        {
                eAppState = value;
        }
}






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

Reply via email to