我正在构建一个我有LogIn活动的应用程序。当用户登录时,我希望当他退出应用程序并再次打开它以跳过LogIn活动并立即进入主要活动上的片段。当我在片段中单击后退按钮时,它会将我发送到LogIn活动。为了解决这个问题,我将finish()放入检查共享首选项令牌中。当用户单击后退按钮时,他不会转到LogIn活动,因为该活动已被终止。但是当我按下" logOut"片段上的按钮它应该将我发送到LogInactivity但是因为该活动被杀死而无效
我的代码是:
主要活动:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sharedPreferences = getSharedPreferences("Token pref", MainActivity.MODE_PRIVATE);
if(sharedPreferences.getString("status", "logged_in").equals("logged_in")){
Intent i = new Intent(MainActivity.this, LoginActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(i);
}
viewToken = (TextView)findViewById(R.id.tokenView);
String data = getIntent().getStringExtra("result");
// viewToken.setText(data);
initializeInjector();
initialize();
}
登录活动:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
SharedPreferences sharedPreferences=getSharedPreferences("Token pref",LoginActivity.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putString("status", "logged_in");
editor.commit();
Intent mainActivity = new Intent(LoginActivity.this, MainActivity.class);
mainActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mainActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
mainActivity.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(mainActivity);
credentials = new Credentials();
login = (Button) findViewById(R.id.btn_login);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
username = (TextInputEditText)findViewById(R.id.username);
password = (TextInputEditText)findViewById(R.id.password);
String getUsername = username.getText().toString();
String getPassword = password.getText().toString();
if (getUsername.length()>0 && getPassword.length()>0 ) {
credentials.setUsername(getUsername);
credentials.setPassword(getPassword);
allOperations();
} else {
Toast.makeText(LoginActivity.this, R.string.empty_fields , Toast.LENGTH_LONG).show();
}
}
});
}
private void check() {
SharedPreferences shf = getSharedPreferences("Token pref", MODE_PRIVATE);
String strPref = shf.getString("token", null);
if (strPref != null) {
Intent i = new Intent(LoginActivity.this, MainActivity.class);
startActivity(i);
finish();
}
}
退出片段:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final Context context = inflater.getContext();
View view = inflater.inflate(R.layout.fragment_wrong_user, container, false);
messageForWrongUser = (TextView)view.findViewById(R.id.wrongUser);
buttonOk = (Button)view.findViewById(R.id.buttonOk);
buttonOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences sharedPreferences = getActivity().getSharedPreferences("Token Prefs", Context.MODE_PRIVATE);
sharedPreferences.edit().remove("token").apply();
Intent intent = new Intent(context, LoginActivity.class);
intent.putExtra("key", "value");
startActivity(intent);
getActivity().finish();
}
});
return view;
}
当用户注销并将其发送到LogIn活动时以及当他下次进入应用程序时他没有注销(只是退出主页按钮上的应用程序)时,我应该做些什么更改?他最后一次来到那里(例如:Fragment)?
对于leon的例子来说这是logcat:
07-21 15:50:49.835 5935-5935/com.telnet.asp E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.IllegalStateException: Fatal Exception thrown on Scheduler.Worker thread.
at rx.android.schedulers.LooperScheduler$ScheduledAction.run(LooperScheduler.java:114)
at android.os.Handler.handleCallback(Handler.java)
at android.os.Handler.dispatchMessage(Handler.java)
at android.os.Looper.loop(Looper.java)
at android.app.ActivityThread.main(ActivityThread.java)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
at dalvik.system.NativeStart.main(Native Method)
Caused by: rx.exceptions.OnErrorFailedException: Error occurred when trying to propagate error to Observer.onError
at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:192)
at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:120)
at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.checkTerminated(OperatorObserveOn.java:276)
at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.call(OperatorObserveOn.java:219)
at rx.android.schedulers.LooperScheduler$ScheduledAction.run(LooperScheduler.java:107)
at android.os.Handler.handleCallback(Handler.java)
at android.os.Handler.dispatchMessage(Handler.java)
at android.os.Looper.loop(Looper.java)
at android.app.ActivityThread.main(ActivityThread.java)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
at dalvik.system.NativeStart.main(Native Method)
Caused by: rx.exceptions.CompositeException: 2 exceptions occurred.
at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:192)
at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:120)
at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.checkTerminated(OperatorObserveOn.java:276)
at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.call(OperatorObserveOn.java:219)
at rx.android.schedulers.LooperScheduler$ScheduledAction.run(LooperScheduler.java:107)
at android.os.Handler.handleCallback(Handler.java)
at android.os.Handler.dispatchMessage(Handler.java)
at android.os.Looper.loop(Looper.java)
at android.app.ActivityThread.main(ActivityThread.java)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
at dalvik.system.NativeStart.main(Native Method)
Caused by: rx.exceptions.CompositeException$CompositeExceptionCausalChain: Chain of Causes for CompositeException In Order Received =>
at android.util.Log.getStackTraceString(Log.java)
at android.util.Slog.e(Slog.java)
at com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
at rx.android.schedulers.LooperScheduler$ScheduledAction.run(LooperScheduler.java:118)
at android.os.Handler.handleCallback(Handler.java)
at android.os.Handler.dispatchMessage(Handler.java)
at android.os.Looper.loop(Looper.java)
at android.app.ActivityThread.main(ActivityThread.java)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
at dalvik.system.NativeStart.main(Native Method)
Caused by: retrofit2.adapter.rxjava.HttpException: HTTP 500 Internal Server Error
at retrofit2.adapter.rxjava.OperatorMapResponseToBodyOrError$1.onNext(OperatorMapResponseToBodyOrError.java:43)
at retrofit2.adapter.rxjava.OperatorMapResponseToBodyOrError$1.onNext(OperatorMapResponseToBodyOrError.java:38)
at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$RequestArbiter.request(RxJavaCallAdapterFactory.java:173)
at rx.internal.operators.OperatorSubscribeOn$1$1$1.request(OperatorSubscribeOn.java:80)
at rx.Subscriber.setProducer(Subscriber.java:211)
at rx.internal.operators.OperatorSubscribeOn$1$1.setProducer(OperatorSubscribeOn.java:76)
at rx.internal.operators.OperatorMap$MapSubscriber.setProducer(OperatorMap.java:99)
at rx.Subscriber.setProducer(Subscriber.java:205)
at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:152)
at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:138)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:50)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:50)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
at rx.Observable.unsafeSubscribe(Observable.java:8666)
at rx.internal.operators.OperatorSubscribeOn$1.call(OperatorSubscribeOn.java:94)
at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
at rx.internal.schedulers.ExecutorScheduler$ExecutorSchedulerWorker.run(ExecutorScheduler.java:104)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.RuntimeException: Duplicate found in causal chain so cropping to prevent loop ...
at android.util.Log.getStackTraceString(Log.java)
at android.util.Slog.e(Slog.java)
at com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
at java.lang.ThreadG
答案 0 :(得分:0)
onClick() of login button use like this
{
Intent i = new Intent(LoginActivity.this, MainActivity.class);
startActivity(i);
finish();
}
// when u r using welcom screen before login, u can do like this. Use shared prefernece. If flag is true then open Mainscreen or else open login screen.
Preferences prfs= new Preferences(this);
if(prfs.getBoolean(KEY)){
Intent i = new Intent(WelcomScreen.this, MainActivity.class);
startActivity(i);
finish();
}
else{
Intent i = new Intent(WelcomScreen.this, LoginScreen.class);
startActivity(i);
finish();
}
答案 1 :(得分:0)
你应该改变这一行
(.createArrayOf
(-> db get-connection :datasource .getConnection)
"varchar"
(into-array String someVector)
)
用这个
SharedPreferences sharedPreferences = getActivity().getSharedPreferences("Token pref", Context.MODE_PRIVATE);
因为您正在创建新的首选项文件,并且那里不存在“令牌”。因此,当用户点击LogOut开始SharedPreferences sharedPreferences = getActivity().getSharedPreferences("Token Prefs", Context.MODE_PRIVATE);
并完成LoginActivity
然后,在MainActivity
中仅检查令牌是否存在
LoginActivity
答案 2 :(得分:0)
让我举一个简单的例子。让我们说你有两个活动 select
CASE
WHEN avg(sm.pace)<0.30 THEN 'Ineligible'
WHEN avg(sm.pace)>=0.30 AND avg(sm.pace)<0.40 THEN 'Tier1'
WHEN avg(sm.pace)>=0.40 AND avg(sm.pace)<0.50 THEN 'Tier2'
WHEN avg(sm.pace)>=0.50 AND avg(sm.pace)<0.75 THEN 'Potential'
WHEN avg(sm.pace)>0.75 THEN 'Inline' END AS Category,
, ROUND(SUM(sm.balance_due),2)
FROM SUMMARY sm
GROUP BY Category
和LoginActivity
。
在MainActivity
中,您可以在LoginActivity
中保存值,如下例所示。
SharedPreferences
在您sharedpreferences = getSharedPreferences("tagName", LoginActivity.this.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("status", "logged_in");
editor.commit();
Intent mainActivity = new Intent(LoginActivity.this, MainActivity.class);
mainActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mainActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
mainActivity.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(mainActivity)
的{{1}}方法中执行此操作,因此当用户按下后退按钮时,只需退出应用
onBackPressed
在LoginActivity
的{{1}}方法中没有,请检查 @Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
值
onCreate
如果值存在则ok,否则将用户重定向到MainActivity
你的退出功能应该是这样的
SharedPreferences
因此,注销后用户将被重定向到 sharedpreferences = getSharedPreferences("tagName", context.MODE_PRIVATE);
//check if value exist
if(sharedpreferences.getString("status", "").toString().equals("")){
Intent i = new Intent(MainActivity.this, LoginActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(i);
}
修改强>
登录活动的LoginActivity
方法如下所示。我认为您正在使用public void logOut(){
SharedPreferences sharedpreferences = getSharedPreferences("tagName", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.clear();
editor.commit();
Intent loginactivity = new Intent(MainActivity.this, LoginActivity.class);
loginactivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
loginactivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
loginactivity.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(loginactivity);
}
方法进行登录过程。在完成登录过程后,请使用代码在LoginActivity
中保存值并重定向到MainActivity。
onCreate
MainActivity的allOperations()
方法将是这样的
SharedPreferences