我有2个活动,一个是通过facebook登录,另一个是家庭活动。我在家庭活动中有一个导航抽屉,其中包含一个名为Logout的项目。我希望新用户通过第一个活动登录,如果他们已经登录,则转到主页活动,然后单击主页活动导航栏中的注销,退出并重定向到登录活动。
这是我的登录活动:
package com.pipipzz.simpleapp;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.login.LoginManager;
import com.facebook.login.LoginResult;
import java.util.Arrays;
public class NewLoginActivity extends Activity {
public static CallbackManager callbackManager;
public boolean signedIn;
public SharedPreferences sharedPreferences;
//public ProfileTracker profileTracker;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Initialize SDK before setContentView(Layout ID)
FacebookSdk.sdkInitialize(getApplicationContext());
sharedPreferences = getPreferences(Context.MODE_PRIVATE);
signedIn = sharedPreferences.getBoolean("signedin", false);
if(signedIn) {
Intent intent = new Intent(NewLoginActivity.this, HomeActivity.class);
startActivity(intent);
//finish();
}
setContentView(R.layout.activity_new_login);
// Initialize layout button
ImageView button = (ImageView) findViewById(R.id.login_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Call private method
onFblogin();
}
});
}
private void onFblogin()
{
callbackManager = CallbackManager.Factory.create();
// Set permissions
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("email", "user_photos", "public_profile"));
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.d("Login Result", "Success");
signedIn = true;
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("signedin", signedIn);
editor.apply();
Intent intent = new Intent(NewLoginActivity.this, HomeActivity.class);
startActivity(intent);
//finish();
}
@Override
public void onCancel() {
Log.d("Login Result", "On cancel");
}
@Override
public void onError(FacebookException error) {
Log.d("Login Result", error.toString());
}
});
// profileTracker = new ProfileTracker() {
// @Override
// protected void onCurrentProfileChanged(Profile profile, Profile profile1) {
// Log.e("oldProfile", profile.getName());
// }
// };
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
//Intent intent = new Intent(NewLoginActivity.this, HomeActivity.class);
//startActivity(intent);
}
}
我正在尝试使用sharedprefrence来存储登录状态。这是我的家庭活动:
package com.pipipzz.simpleapp;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
public class HomeActivity extends AppCompatActivity {
Toolbar toolbar;
ViewPager viewPager;
ViewPagerAdapter viewPagerAdapter;
SlidingTabLayout slidingTabLayout;
CharSequence Titles[] = {"Following", "Trending"};
int numOfTabs = 2;
private ActionBarDrawerToggle drawerToggle;
private DrawerLayout mDrawer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
//Initialise views
viewPager = (ViewPager) findViewById(R.id.pager);
slidingTabLayout = (SlidingTabLayout) findViewById(R.id.tabs);
toolbar = (Toolbar) findViewById(R.id.toolbar1);
setSupportActionBar(toolbar);
final ActionBar ab = getSupportActionBar();
assert ab != null;
ab.setHomeButtonEnabled(true);
ab.setDisplayHomeAsUpEnabled(true);
ab.setHomeAsUpIndicator(R.drawable.menu);
viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), Titles, numOfTabs);
viewPager.setAdapter(viewPagerAdapter);
slidingTabLayout.setDistributeEvenly(true);
slidingTabLayout.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
//return Color.parseColor("#88ffffff");
return Color.parseColor("#FAC80A");
}
});
slidingTabLayout.setViewPager(viewPager);
if (android.os.Build.VERSION.SDK_INT >= 21)
{
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(Color.parseColor("#263238"));
}
FloatingActionButton button = (FloatingActionButton) findViewById(R.id.ddd);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(HomeActivity.this, ImageGridActivity.class);
startActivity(intent);
}
});
// Find our drawer view
mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerToggle = setupDrawerToggle();
drawerToggle.setDrawerIndicatorEnabled(false);
toolbar.setNavigationIcon(R.drawable.menu);
mDrawer.setDrawerListener(drawerToggle);
// Find our drawer view
NavigationView nvDrawer = (NavigationView) findViewById(R.id.nvView);
// Setup drawer view
setupDrawerContent(nvDrawer);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mDrawer.openDrawer(Gravity.LEFT);
}
});
}
private ActionBarDrawerToggle setupDrawerToggle() {
return new ActionBarDrawerToggle(this, mDrawer, toolbar, R.string.drawer_open, R.string.drawer_close);
}
private void setupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
selectDrawerItem(menuItem);
return true;
}
});
}
public void selectDrawerItem(MenuItem menuItem) {
// Create a new fragment and specify the planet to show based on
// position
Fragment fragment = null;
Class fragmentClass;
switch(menuItem.getItemId()) {
case R.id.nav_first_fragment:
fragmentClass = MyFeedFragment.class;
break;
case R.id.nav_second_fragment:
fragmentClass = SurpriseMeFragment.class;
break;
case R.id.nav_third_fragment:
fragmentClass = GalleriesFragment.class;
break;
case R.id.nav_fourth_fragment:
fragmentClass = MyProfileFragment.class;
break;
case R.id.nav_fifth_fragment:
fragmentClass = MessagesFragment.class;
break;
case R.id.nav_settings:
fragmentClass = SettingsFragment.class;
break;
case R.id.nav_logout:
fragmentClass = LogoutFragment.class;
break;
default:
fragmentClass = MyFeedFragment.class;
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = this.getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent, fragment).addToBackStack(null).commit();
// Highlight the selected item, update the title, and close the drawer
menuItem.setChecked(true);
//setTitle(menuItem.getTitle());
mDrawer.closeDrawers();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.menu_home, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
mDrawer.openDrawer(GravityCompat.START);
return true;
}
// if (drawerToggle.onOptionsItemSelected(item)) {
// return true;
// }
return super.onOptionsItemSelected(item);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
drawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggles
drawerToggle.onConfigurationChanged(newConfig);
}
我不想在导航抽屉中点击退出时绘制任何UI,因此我创建了一个无用于注销的UI片段:
package com.pipipzz.simpleapp;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import com.facebook.AccessToken;
import com.facebook.login.LoginManager;
/**
* A simple {@link Fragment} subclass.
*/
public class LogoutFragment extends Fragment {
public LogoutFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
if(AccessToken.getCurrentAccessToken() != null) {
LoginManager.getInstance().logOut();
}
SharedPreferences sharedPreferences = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
//editor.clear();
editor.putBoolean("signedin", false);
editor.apply();
Intent intent = new Intent(getActivity(), NewLoginActivity.class);
startActivity(intent);
}
}
在执行上述操作后,我可以注销facebook,但我无法重定向到登录活动。我经常搜索,但找不到任何东西。可能是什么问题?请帮忙。