안드로이드 MyFacebookApp 프로젝트 2

(안드로이드 MyFacebookApp 프로젝트 개선에서 넘어옴)

1 개요[ | ]

안드로이드 MyFacebookApp 프로젝트 개선
  • MainActivity 대신 MainFragment가 activity_main.xml을 사용하게 함
  • MainFragment 상태 변화시 UiLifecycleHelper 상태도 똑같이 맞춰줌

2 사전 작업[ | ]

3 MainFragment.java 생성[ | ]

  • ADT --- MyFacebookApp --- src --- com.example.myfacebookapp 우클릭 --- New --- Class
  • "New Java Class" --- Name: MainFragment --- Superclass: android.support.v4.app.Fragment --- [Finish]
  • 파일 내용을 다음으로 교체
package com.example.myfacebookapp;

import java.util.Arrays;

import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;
import com.facebook.model.GraphUser;
import com.facebook.widget.LoginButton;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.LayoutInflater;
import android.widget.TextView;

public class MainFragment extends Fragment {
	private UiLifecycleHelper uiHelper;
	private static final String TAG = "MainFragment";
	private View mainView; 
	private Session.StatusCallback callback = new Session.StatusCallback() {
		@Override
		public void call(Session session, SessionState state, Exception exception) {
			onSessionStateChange(session, state, exception);
		}
	};

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		mainView = inflater.inflate(R.layout.activity_main, container, false);

		LoginButton authButton = (LoginButton) mainView.findViewById(R.id.authButton);
		authButton.setFragment(this);
		authButton.setReadPermissions(Arrays.asList("user_likes", "user_status"));

		return mainView;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		uiHelper = new UiLifecycleHelper(getActivity(), callback);
		uiHelper.onCreate(savedInstanceState);
	}

	private void onSessionStateChange(Session session, SessionState state, Exception exception) {
		if (state.isOpened()) {
			Log.i(TAG, "Logged In...");
			Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {

				// callback after Graph API response with user object
				@Override
				public void onCompleted(GraphUser user, Response response) {
					if (user != null) {
						//Log.i("user.getName()", user.getName());
						TextView textView1 = (TextView) mainView.findViewById(R.id.textView1);
						textView1.setText("Hello [" + user.getName() + "]");
					}
				}
			});
		} else if (state.isClosed()) {
			Log.i(TAG, "Logged out...");
			TextView textView1 = (TextView) mainView.findViewById(R.id.textView1);
			textView1.setText("Hello [anonymous]");
		}
	}

	@Override
	public void onResume() {
		super.onResume();
		Session session = Session.getActiveSession();
		if (session != null && (session.isOpened() || session.isClosed()) ) {
			onSessionStateChange(session, session.getState(), null);
		}
		uiHelper.onResume();
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		uiHelper.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public void onPause() {
		super.onPause();
		uiHelper.onPause();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		uiHelper.onDestroy();
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		uiHelper.onSaveInstanceState(outState);
	}
}

4 MainActivity.java 교체[ | ]

package com.example.myfacebookapp;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {
	private MainFragment mainFragment;

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

	    if (savedInstanceState == null) {
	    	// Add the fragment on initial activity setup
	        mainFragment = new MainFragment();
	        getSupportFragmentManager()
	        .beginTransaction()
	        .add(android.R.id.content, mainFragment)
	        .commit();
	    } else {
	        // Or set the fragment from restored state info
	        mainFragment = (MainFragment) getSupportFragmentManager()
	        .findFragmentById(android.R.id.content);
	    }
	}
}

5 실행[ | ]

  • Ctrl+F11 눌러 실행
  • 이제 로그인 상태와 이름 표시 상태가 잘 들어맞는다.
(다만, 로그인 시도할 때 통신으로 인해 약간의 지연은 있다.)

6 같이 보기[ | ]

7 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}