본문 바로가기

Android

안드로이드 멀티 스크린 엑티비티 ( 한화면에서 여러 엑티비티 제어하기 )

보통 안드로이드에서는 엑비티를 호출할때 새로운 화면으로 엑티비티를 가져옵니다. 하지만 아래에 사용한 방법이라면 하나의 엑티비티안에서 새로운 화면을 생성하지 않고 여러 엑티비티를 관리 할 수 있습니다. 호스트클래스를 두고 새로사용되는 엑티비티를 스텍으로 하여 쌓아놓고 취소 시에 이전 엑티비티를 불러오는 방식입니다.

호스트 부분

package dev.focusone.multiScreen;


import java.util.Stack;


import android.app.Activity;

import android.os.Bundle;


public class MultiScreenHostActivity extends Activity {

    /** Called when the activity is first created. */

// Navigation Stack

protected Stack<MultiScreenActivity> m_MultiActivityStack;

// 현재 액티비티

protected MultiScreenActivity m_TopActivity;

// Not extendable LifeCycle 

/* * * * * * * * * * * * * * * * * * * * * * * * */

    @Override

    final protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        m_MultiActivityStack = new Stack<MultiScreenActivity>();

        this.onCreateEX(savedInstanceState);

    }

    

    

@Override

final protected void onStart() {

// TODO Auto-generated method stub

super.onStart();

if ( m_TopActivity == null)

return ;

m_TopActivity.onStart();

this.onStartEX();

}


@Override

final protected void onResume() {

// TODO Auto-generated method stub

super.onResume();

if ( m_TopActivity == null)

return ;

m_TopActivity.onResume();

this.onResumeEX();

}


@Override

final protected void onPause() {

// TODO Auto-generated method stub

super.onPause();

if ( m_TopActivity == null)

return ;

m_TopActivity.onPause();

this.onPauseEX();

}


final protected void onStop() {

super.onStop();

if ( m_TopActivity == null)

return ;

m_TopActivity.onStop();

this.onStopEX();

}

@Override

final protected void onDestroy() {

// TODO Auto-generated method stub

super.onDestroy();

if ( m_TopActivity == null)

return ;

m_TopActivity.onDestroy();

this.onDestroyEX();

}


@Override

final public void onBackPressed() {

// TODO Auto-generated method stub

if ( m_TopActivity == null)

return ;

m_TopActivity.onBackPressed();

this.onBackPressedEX();

}


/**

* 첫번째 Activity를 세팅한다.

* @param activity 새로운 MultiScreenActivity인스턴스.

* @throws Exception 

*/

final public void setFirstActivity(MultiScreenActivity activity) throws Exception {

if ( m_MultiActivityStack.size() != 0 )

        throw new Exception("Already did setting");

        

        m_MultiActivityStack.push(activity);

        m_TopActivity = activity;

        m_TopActivity.onCreate(null);

    }

// Extendable LifeCycle 

/* * * * * * * * * * * * * * * * * * * * * * * * */

    protected void onCreateEX(Bundle savedInstanceState) {}

protected void onStartEX() {}

protected void onResumeEX() {}

protected void onPauseEX() {}

protected void onStopEX() {}

protected void onDestroyEX() {}

public void onBackPressedEX() {}


}



 스크린 부분

package dev.focusone.multiScreen;


import android.os.Bundle;

import android.view.View;


abstract public class MultiScreenActivity {

 

// 적용할 화면에 대한 Activity타입이어야한다.

private View m_View;

private boolean isCreated; // 이 액티비티가 한번 만들어졌는지에 대한 체크

private MultiScreenHostActivity m_HostActivity;

public MultiScreenActivity (MultiScreenHostActivity activity) {

m_HostActivity = activity;

}

// Multi Activity LifeCycle

protected void onCreate(Bundle savedInstanceState) {

isCreated = true;

}

    protected void onStart(){}

    //재호출 할 경우 view를 다시 세팅 

protected void onResume() {

m_HostActivity.setContentView(m_View);

}

protected void onPause(){}

protected void onStop(){}

protected void onDestroy(){}

// View Setting Method

public void setContentsView(int resource) {

m_View = View.inflate(m_HostActivity, resource, null);

m_HostActivity.setContentView(m_View);

}

public View findViewById(int id) {

return m_View.findViewById(id);

}

protected MultiScreenHostActivity getHostActivity() {

return m_HostActivity;

}

protected synchronized void startActivity(MultiScreenActivity activity) {

m_HostActivity.m_MultiActivityStack.push(activity);

m_HostActivity.m_TopActivity = activity;

activity.onCreate(null);

activity.onStart();

activity.onResume();

}

protected synchronized void finish() {

MultiScreenActivity popActivity = m_HostActivity.m_MultiActivityStack.pop();

if ( m_HostActivity.m_MultiActivityStack.size() == 0 )

m_HostActivity.finish();

else {

MultiScreenActivity pushActivity = m_HostActivity.m_MultiActivityStack.lastElement();

popActivity.onPause();

popActivity.onStop();

popActivity.onDestroy();

if ( !pushActivity.isCreated ) {

pushActivity.onCreate(null);

pushActivity.onStart();

}

m_HostActivity.m_TopActivity = pushActivity;

pushActivity.onResume();

}

}

protected void onBackPressed() {

finish();

}

}

 
 
호스트 상속 클래스

package dev.focusone;


import android.os.Bundle;

import dev.focusone.multiScreen.MultiScreenHostActivity;


public class HostActivity extends MultiScreenHostActivity {

@Override

protected void onCreateEX(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreateEX(savedInstanceState);

try {

setFirstActivity(new FirstScreen(this));

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}


@Override

protected void onStartEX() {

// TODO Auto-generated method stub

super.onStartEX();

}


@Override

protected void onResumeEX() {

// TODO Auto-generated method stub

super.onResumeEX();

}


@Override

protected void onPauseEX() {

// TODO Auto-generated method stub

super.onPauseEX();

}


@Override

protected void onStopEX() {

// TODO Auto-generated method stub

super.onStopEX();

}


@Override

protected void onDestroyEX() {

// TODO Auto-generated method stub

super.onDestroyEX();

}


@Override

public void onBackPressedEX() {

// TODO Auto-generated method stub

super.onBackPressedEX();

}

}

 
첫번째 화면

package dev.focusone;


import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import dev.focusone.multiScreen.MultiScreenActivity;

import dev.focusone.multiScreen.MultiScreenHostActivity;


public class FirstScreen extends MultiScreenActivity implements OnClickListener{



private Button m_Button;


public FirstScreen(MultiScreenHostActivity activity) {

super(activity);

// TODO Auto-generated constructor stub

}


@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

setContentsView(R.layout.first);

m_Button = (Button) findViewById(R.id.first_button_noName);

m_Button.setOnClickListener(this);

}


@Override

protected void onStart() {

// TODO Auto-generated method stub

}


@Override

protected void onResume() {

// TODO Auto-generated method stub

}


@Override

protected void onPause() {

// TODO Auto-generated method stub

}


@Override

protected void onStop() {

// TODO Auto-generated method stub

}


@Override

protected void onDestroy() {

// TODO Auto-generated method stub

}

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

startActivity(new SecondScreen(getHostActivity()));

}

}


 
두번째 화면

package dev.focusone;


import android.os.Bundle;

import dev.focusone.multiScreen.MultiScreenActivity;

import dev.focusone.multiScreen.MultiScreenHostActivity;


public class SecondScreen extends MultiScreenActivity {


public SecondScreen(MultiScreenHostActivity activity) {

super(activity);

// TODO Auto-generated constructor stub

}


@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

setContentsView(R.layout.second);

}


@Override

protected void onStart() {

// TODO Auto-generated method stub

}


@Override

protected void onResume() {

// TODO Auto-generated method stub

}


@Override

protected void onPause() {

// TODO Auto-generated method stub

}


@Override

protected void onStop() {

// TODO Auto-generated method stub

}


@Override

protected void onDestroy() {

// TODO Auto-generated method stub

}

}