我有以下问题: 当用户在v4片段内按下登录按钮时,弹出的权限对话框显示,片段内容不可见,在我接受了权限后,活动停止。
清单的代码是:
<!-- To auto-complete the email text field in the login form with the-->
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<!-- To access Google+ APIs: -->
<uses-permission android:name="android.permission.INTERNET" />
<application
android:label="@string/app_name"
android:largeHeap="true"
android:theme="@style/AppTheme">
<activity
android:name=".LoginActivity"
android:noHistory="true"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateVisible|adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
片段的代码是:
package demo.android.checkingforpermission;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class LoginFragment extends Fragment implements
View.OnClickListener {
private static final int PERMISSIONS_REQUEST_READ_CONTACTS = 101;
public LoginFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup
container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_login, container,
false);
//Find the +1 button
Button login = (Button) view.findViewById(R.id.login);
login.setOnClickListener(this);
return view;
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.login) {
checkForLoginPermissions();
}
}
private void checkForLoginPermissions() {
boolean hasPermission =
(ContextCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_NETWORK_STATE) == PackageManager.PERMISSION_GRANTED);
if (hasPermission) {
hasPermission = (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED);
}
if (!hasPermission) {
requestPermissions(
new String[]
{android.Manifest.permission.ACCESS_NETWORK_STATE,
Manifest.permission.READ_PHONE_STATE}, PERMISSIONS_REQUEST_READ_CONTACTS);
} else {
doShowContacts();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[]
permissions, int[] grantResults) {
if (requestCode == PERMISSIONS_REQUEST_READ_CONTACTS &&
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
doShowContacts();
}
}
private void doShowContacts() {
Toast.makeText(getContext(), "Login ok",
Toast.LENGTH_SHORT).show();
}
}
//活动代码是:
package demo.android.checkingforpermission;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
/**
* A login screen that offers login via email/password.
*/
public class LoginActivity extends FragmentActivity {
private static final String CURRENT_FRAGMENT =
"CURRENT_FRAGMENT";
private static final String TAG =
LoginActivity.class.toString();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// Check whether the activity is using the layout version
with
// the fragment_container FrameLayout. If so, we must add
the first fragment
if (findViewById(R.id.container) != null) {
// However, if we're being restored from a previous
state,
// then we don't need to do anything and should return
or else
// we could end up with overlapping fragments.
if (savedInstanceState != null) {
return;
}
FragmentManager fragmentManager =
getSupportFragmentManager();
FragmentTransaction transaction =
fragmentManager.beginTransaction();
transaction.replace(R.id.container, new
LoginFragment());
transaction.add(R.id.container, new LoginFragment(),
CURRENT_FRAGMENT);
transaction.commitAllowingStateLoss();
}
}
}
// xml文件是: // 1. fragment_login.xml
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="demo.android.checkingforpermission.LoginFragment">
<Button
android:layout_gravity="bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:id="@+id/login"
/>
</FrameLayout>
//and activity_login.xml
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LoginActivity"/>
日志文件是:
09-28 19:36:31.612 8249-8249/android.demo.checkingforpermission D/TimaKeyStoreProvider: TimaSignature is unavailable
09-28 19:36:31.612 8249-8249/android.demo.checkingforpermission D/ActivityThread: Added TimaKeyStore provider
09-28 19:36:31.692 8249-8249/android.demo.checkingforpermission W/ResourcesManager: getTopLevelResources: /data/app/android.demo.checkingforpermission-1/base.apk / 1.0 running in android.demo.checkingforpermission rsrc of package android.demo.checkingforpermission
09-28 19:36:31.692 8249-8249/android.demo.checkingforpermission I/InjectionManager: Inside getClassLibPath + mLibMap{0=, 1=}
09-28 19:36:31.692 8249-8249/android.demo.checkingforpermission D/ResourcesManager: For user 0 new overlays fetched Null
09-28 19:36:31.702 8249-8249/android.demo.checkingforpermission I/InjectionManager: Inside getClassLibPath caller
09-28 19:36:31.712 8249-8249/android.demo.checkingforpermission W/System: ClassLoader referenced unknown path: /data/app/android.demo.checkingforpermission-1/lib/arm64
09-28 19:36:31.722 8249-8249/android.demo.checkingforpermission D/InjectionManager: InjectionManager
09-28 19:36:31.722 8249-8249/android.demo.checkingforpermission D/InjectionManager: fillFeatureStoreMap android.demo.checkingforpermission
09-28 19:36:31.722 8249-8249/android.demo.checkingforpermission I/InjectionManager: Constructor android.demo.checkingforpermission, Feature store :{}
09-28 19:36:31.722 8249-8249/android.demo.checkingforpermission I/InjectionManager: featureStore :{}
09-28 19:36:31.742 8249-8249/android.demo.checkingforpermission W/ResourcesManager: getTopLevelResources: /data/app/android.demo.checkingforpermission-1/base.apk / 1.0 running in android.demo.checkingforpermission rsrc of package android.demo.checkingforpermission
09-28 19:36:31.742 8249-8249/android.demo.checkingforpermission W/ResourcesManager: getTopLevelResources: /data/app/android.demo.checkingforpermission-1/base.apk / 1.0 running in android.demo.checkingforpermission rsrc of package android.demo.checkingforpermission
09-28 19:36:31.782 8249-8249/android.demo.checkingforpermission D/Activity: performCreate Call Injection manager
09-28 19:36:31.792 8249-8249/android.demo.checkingforpermission I/InjectionManager: dispatchOnViewCreated > Target : android.demo.checkingforpermission.LoginActivity isFragment :false
09-28 19:36:31.812 8249-8249/android.demo.checkingforpermission D/SecWifiDisplayUtil: Metadata value : SecSettings2
09-28 19:36:31.822 8249-8249/android.demo.checkingforpermission D/ViewRootImpl: #1 mView = com.android.internal.policy.PhoneWindow$DecorView{37e4ea5 I.E...... R.....ID 0,0-0,0}
09-28 19:36:31.822 8249-8309/android.demo.checkingforpermission D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
09-28 19:36:31.872 8249-8309/android.demo.checkingforpermission D/libEGL: loaded /vendor/lib64/egl/libGLES_mali.so
09-28 19:36:31.892 8249-8309/android.demo.checkingforpermission D/libEGL: eglInitialize EGLDisplay = 0x7fa88df178
09-28 19:36:31.892 8249-8309/android.demo.checkingforpermission I/OpenGLRenderer: Initialized EGL, version 1.4
[ 09-28 19:36:31.892 8249: 8309 D/ ]
ro.exynos.dss isEnabled: 0
09-28 19:36:31.902 8249-8309/android.demo.checkingforpermission D/mali_winsys: new_window_surface returns 0x3000, [1440x2560]-format:1
09-28 19:36:31.912 8249-8249/android.demo.checkingforpermission D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 96 - 0, 0) vi=Rect(0, 96 - 0, 0) or=1
09-28 19:36:31.922 8249-8309/android.demo.checkingforpermission D/libGLESv1: DTS_GLAPI : DTS is not allowed for Package : android.demo.checkingforpermission
09-28 19:36:32.052 8249-8249/android.demo.checkingforpermission I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@376d83c time:62792654
09-28 19:36:32.262 8249-8249/android.demo.checkingforpermission D/ViewRootImpl: MSG_RESIZED: ci=Rect(0, 96 - 0, 1046) vi=Rect(0, 96 - 0, 1046) or=1
09-28 19:36:36.772 8249-8249/android.demo.checkingforpermission I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@376d83c time:62797370
09-28 19:36:36.822 8249-8249/android.demo.checkingforpermission D/ViewRootImpl: The input has been finished in ImeInputStage.
09-28 19:36:36.862 8249-8249/android.demo.checkingforpermission D/ViewRootImpl: The input has been finished in ImeInputStage.
09-28 19:36:36.942 8249-8249/android.demo.checkingforpermission D/ViewRootImpl: MSG_RESIZED: ci=Rect(0, 96 - 0, 0) vi=Rect(0, 96 - 0, 0) or=1
09-28 19:36:39.742 8249-8249/android.demo.checkingforpermission D/ViewRootImpl: ViewPostImeInputStage processPointer 0
09-28 19:36:39.802 8249-8249/android.demo.checkingforpermission D/ViewRootImpl: ViewPostImeInputStage processPointer 1
09-28 19:36:40.582 8249-8309/android.demo.checkingforpermission D/OpenGLRenderer: endAllActiveAnimators on 0x7f92a45400 (RippleDrawable) with handle 0x7fa1c69940
09-28 19:36:40.582 8249-8249/android.demo.checkingforpermission D/ViewRootImpl: #3 mView = null
答案 0 :(得分:1)
我删除了
android:noHistory="true"
来自清单的标签和工作。