Android에서 로그인 화면 / 활동을 만드는 올바른 방법은 무엇입니까?
다른 작업을 수행하기 전에 사용자가 로그인해야하는 Android 애플리케이션을 개발 중입니다. 현재 LoginScreen이라는 기본 활동을 만들었으며 성공적으로 로그인하면이 활동이 홈이라는 다른 활동을 시작합니다. 하지만이 접근 방식에 문제가 있습니다. 사용자가 홈 활동에서 뒤로 버튼을 누르면 어떻게 되나요? 사용자가 로그인 화면으로 돌아가는 것을 원하지 않습니다. 사용자가이를 중지하는 올바른 방법은 무엇입니까? 키 누르기 이벤트를 처리해야합니까?
내가 한 일은 내 홈 활동이 android.intent.action.MAIN 인 텐트를 처리하도록 만드는 것이 었습니다 . 홈 활동이 시작되면 사용자가 로그인했는지 여부 (공유 환경 설정 사용)를 확인하고 그렇지 않은 경우 LoginActivity를 시작하고 자체적으로 finish ()를 호출합니다.
로그인 성공시 LoginActivity는 Main 활동을 시작하며 이번에는 사용자가 로그온했기 때문에 Main 활동이 정상적인 과정을 계속합니다. LoginActivity는 매니페스트 파일에서 다음과 같이 선언됩니다.
<activity android:name="LoginScreen" android:label="@string/app_name"
android:noHistory="true" android:excludeFromRecents="true">
</activity>
LoginActivity에 대해 noHistory 및 excludeFromRecents를 true로 설정하면 사용자가 뒤로 버튼을 사용하여이 활동으로 돌아갈 수 없습니다.
startActivity(...)
LoginScreen 활동 을 호출 한 후 finish()
. 이렇게하면 활동 스택에서 해당 활동이 제거되므로 홈 활동에서 뒤로를 누르면 기본적으로 앱이 닫힙니다.
플래그를 의도로 설정해보십시오.
예:
new Intent(context, SomeActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
플래그에 대한 추가 정보 : http://developer.android.com/reference/android/content/Intent.html#nestedclasses
참조 : https://stackoverflow.com/a/41290453/4560689 (아래 텍스트)
이렇게하려면 홈 화면으로 이동할 것인지 로그인 / 등록 할 것인지 논리를 실행하는 디스플레이 없음 (Android의 NoDisplay 테마 사용)이있는 단일 런처 활동을 만들어야합니다.
먼저 매니페스트에서 :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.android">
<-- Permissions etc -->
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name">
<activity
android:name=".onboarding.StartupActivity"
android:label="@string/app_name"
android:launchMode="singleInstance"
android:theme="android:style/Theme.NoDisplay">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:launchMode="singleTop" />
<activity
android:name=".authentication.controller.AuthenticationActivity"
android:label="@string/title_sign_in"
android:launchMode="singleTask"
android:windowSoftInputMode="adjustResize|stateHidden" />
<-- Other activities, services, etc -->
</application>
그런 다음 StartupActivity :
package com.example.android.onboarding;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import com.example.android.MainActivity;
import com.example.android.authentication.controller.AuthenticationActivity;
import com.example.android.util.ResourceUtils;
public class StartupActivity extends Activity {
private static final AUTHENTICATION_REQUEST_CODE = 1000;
@Override
protected void onCreate(Bundle savedInstanceState) {
if (isLoggedIn()) {
Intent startupIntent = new Intent(this, MainActivity.class);
startupIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(startupIntent);
finish();
} else {
Intent startupIntent = new Intent(this, AuthenticationActivity.class);
startActivityForResult(startupIntent, AUTHENTICATION_REQUEST_CODE);
}
super.onCreate(savedInstanceState);
}
private boolean isLoggedIn() {
// Check SharedPreferences or wherever you store login information
return this.getSharedPreferences("my_app_preferences", Context.MODE_PRIVATE).getBoolean("loggedIn", false);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == AUTHENTICATION_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
Intent startupIntent = new Intent(this, MainActivity.class);
startupIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(startupIntent);
}
finish();
}
}
요점 : https://gist.github.com/chanakin/c44bf1c6a9a80d2640440b5aaa92c8ee
LoginActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fitsSystemWindows="true"
android:background="#263238">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="80dp"
android:paddingLeft="16dp"
android:paddingRight="16dp">
<!-- App Logo -->
<ImageView android:id="@+id/logo"
android:src="@drawable/logo"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginBottom="20dp"
android:layout_gravity="center_horizontal" />
<!--Title TextView-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="STOCK BUDDY"
android:id="@+id/title"
android:textSize="24sp"
android:textStyle="bold"
android:textColor="#7B869B"
android:layout_marginBottom="24dp"
android:layout_gravity="center_horizontal"/>
<!--User Email-->
<EditText
android:id="@+id/login_email"
android:layout_marginTop="10dp"
android:layout_marginBottom="5dp"
android:layout_centerVertical="true"
android:layout_width="match_parent"
android:layout_height="40dp"
android:ellipsize="start"
android:gravity="center"
android:hint="Email"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textColorHint="#cccccc"
android:textColor="#7B869B"
android:maxLength="40"
android:maxLines="1"
android:inputType="textEmailAddress"
android:background="@drawable/edittextshape"/>
<!-- User Password -->
<EditText
android:id="@+id/login_password"
android:layout_marginTop="5dp"
android:layout_marginBottom="10dp"
android:layout_centerVertical="true"
android:layout_width="match_parent"
android:layout_height="40dp"
android:ellipsize="start"
android:gravity="center"
android:paddingRight="16dp"
android:paddingLeft="16dp"
android:hint="Password"
android:textColor="#7B869B"
android:textColorHint="#cccccc"
android:maxLength="20"
android:maxLines="1"
android:inputType="textPassword"
android:background="@drawable/edittextshape"/>
<!--Login Button-->
<android.support.v7.widget.AppCompatButton
android:id="@+id/btn_login"
android:layout_width="fill_parent"
android:layout_marginTop="5dp"
android:layout_marginBottom="24dp"
android:background="@drawable/buttonshape"
android:text="Login"
android:textSize="20sp"
android:layout_height="40dp"
android:textColor="#ffffff"
android:shadowRadius="5"
android:onClick="Login"/>
<!--signup Link TextView-->
<TextView android:id="@+id/link_signup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:text="No account yet? Create one"
android:gravity="center"
android:textSize="12sp"
android:textColor="#7B869B"/>
</LinearLayout>
</ScrollView>
buttonshape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
android:radius="44dp"
/>
<gradient
android:angle="45"
android:centerX="35%"
android:centerColor="#63D0C3"
android:startColor="#70DB9A"
android:endColor="#56C5EE"
android:type="linear"
/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
/>
<stroke
android:width="0dp"
android:color="#878787"
/>
</shape>
edittextshape.xml
<?xml version="1.0" encoding="utf-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:thickness="0dp"
android:shape="rectangle">
<solid android:color="#ffffff"/>
<stroke android:width="1dp"
android:color="#ffffff" />
<corners android:radius="44dp" />
</shape>
.....................
https://androidpugnator.wordpress.com/2017/03/12/android-login-and-signup-screens에서 전체 코드를 참조하십시오
일부 이벤트에 대한 LoginActivity에서 startActivity (...) 를 호출합니다 (예 : 로그인 버튼 클릭). 별도의 데이터베이스 클래스를 사용하여 HomeActivity 클래스에서 사용자의 사용자 이름과 비밀번호를 저장하십시오.
Handle the onKeyDown() event for controlling back button in HomeActivity(use finish() method).
In OnCreate() method of LoginActivity class use database connection to check whether the username & password already exist in the database table if yes then call startActivity() there too to directly go to HomeScreen from LoginScreen.This will not show the LoginScreen.
Hope this will work for you. Try it.
ReferenceURL : https://stackoverflow.com/questions/5859095/what-is-the-correct-way-of-creating-a-login-screen-activity-in-android
'developer tip' 카테고리의 다른 글
Android의 VideoView에서 비디오 재생 (0) | 2020.12.31 |
---|---|
Xcode를 업데이트하는 방법은 무엇입니까? (0) | 2020.12.31 |
쉬운 배포 및 업데이트를위한 Node.js 설정 (0) | 2020.12.31 |
RequireJS에서 동적 요구 사항, "컨텍스트에 대한 모듈 이름이 아직로드되지 않았습니다"오류가 발생합니까? (0) | 2020.12.31 |
abs (double)에 대한 모호한 오버로드 호출 (0) | 2020.12.30 |