标签: android-lifecycle

如何在Robotium中检查应用程序是否已完成?

我正在尝试使用Robotium创建一种测试方法,以检查单击按钮后Android应用程序是否完成(在代码中,finish()当用户点击该按钮时便有一个调用)。

public void test_onclickExit_finish() {
    String buttonText = resources.getString(R.string.exit);
    Button exitButton = solo.getButton(buttonText, true);
    solo.clickOnView(exitButton);
    // check here that the app has finished
    // wait for the activity to finish?
    assertTrue(solo.getCurrentActivity() == null);
}
Run Code Online (Sandbox Code Playgroud)

但是这个测试失败了。我不知道如何指示测试要等到活动结束。另外,我不确定使用getCurrentActivity()是否是检查应用程序是否完成的好方法。

如何检查申请/活动是否完成?

谢谢。

testing android robotium android-lifecycle

3
推荐指数
1
解决办法
1912
查看次数

片段:测量视图的最佳位置是什么?

我有一个片段,我需要在屏幕上测量其视图的位置/宽度/高度,并传递给其他类.

所以我所拥有的是一个功能,它是这样的:

private void measureTest(){
    v = ourView.findViewById(R.id.someTextField);
    v.getLocationOnScreen(loc);
    int w = v.getWidth();
    ...
    SomeClass.passLocation(loc,w);
    ...
Run Code Online (Sandbox Code Playgroud)

问题是视图的位置/宽度/高度在片段生命周期内没有准备好.所以,如果我在这些生命周期方法中运行该函数:

onCreateView
onViewCreated
onStart
onResume
Run Code Online (Sandbox Code Playgroud)

我要么得到错误的位置和宽度/高度测量值或0值.我找到的唯一解决方案是GlobalLayoutListener向mainView 添加这样的解决方案

mainView.getViewTreeObserver().addOnGlobalLayoutListener(new     ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
    if(alreadyMeasured)
        mainView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
    else
        measureTest();
}
});
Run Code Online (Sandbox Code Playgroud)

这可以完成工作..但它只是Yack!IMO.

有没有更好的方法呢?看起来像是一件基本的事情

android android-layout android-lifecycle android-fragments android-view

3
推荐指数
1
解决办法
2653
查看次数

模拟 Activity/Fragment 重新创建

我正在尝试模拟ActivityFragment重新创建,并且还检查onSaveInstancestate()并且onRestoreInstanceState()通常检查我是否在所有情况下都以良好的方式处理活动重新创建,例如,就像屏幕旋转导致活动重新创建它时一样自己。

\n\n

但就我而言,我想检查更多可能导致重新创建的选项/案例,因为我的应用程序无法旋转(所有活动均为纵向)。

\n\n

我看到了很多关于这个主题的文章、博客和 stackoverflow 问题/答案,例如这里、 这里这里。

\n\n

正如这个 stackoverflow 答案所说,为什么不总是使用 android:configChanges="keyboardHidden|orientation" ?\n 还有更多的事件可能会导致活动重新创建,因此在我读完它之后,我想测试我的应用程序中的一些事件。

\n\n

例如,我在活动中按下主页按钮,然后进入设置并尝试更改语言、更改字体大小等...,但这些操作都没有使我的应用程序按照我的预期重新创建。\ n当我返回我的应用程序时,它只是恢复并onCreate()从未调用。\n所以我什至检查了有关此的官方文档。他们还说这应该导致我的活动重新创建:引用:

\n\n
"When a configuration change occurs at runtime, the activity is shut down and restarted by default" \n
Run Code Online (Sandbox Code Playgroud)\n\n

但正如我所说,这并没有发生在我身上。

\n\n

这对我来说很重要,因为我非常 na\xc3\xafve 并认为如果我的应用程序仅以纵向显示或者我是否将这一行添加到清单中:

\n\n
android:configChanges="keyboardHidden|orientation|screenSize"\n
Run Code Online (Sandbox Code Playgroud)\n\n

那么一切都会好起来的,显然不是因为有更多的配置更改可以重新启动我的活动,所以我不能再运行它,我想以良好的方式处理它,现在我也想测试它。

\n

android android-lifecycle android-fragments android-activity

3
推荐指数
1
解决办法
1560
查看次数

View.getContext返回null,为什么?

我正在使用带有javascript界面​​的 WebView,有时当我loadUrl在webView上调用时,mWebView.getContext()返回null.

  1. 视图怎么没有Context?为什么?
  2. 视图whitout上下文是否仍在使用或可重用?
  3. 当我的观点没有背景时,我该怎么办?
  4. 最重要的是,如果视图没有上下文,会mWebView.post(new Runnable() { ...被执行吗?
  5. 这段代码是否相关?

    if (Looper.getMainLooper().getThread() == Thread.currentThread()) {
        mWebView.loadUrl("javascript:...");
    } else {
        mWebView.post(new Runnable() {
            public void run() {
                mWebView.loadUrl("javascript:...");
            }
        });
    }
    
    Run Code Online (Sandbox Code Playgroud)

android android-lifecycle android-view

3
推荐指数
1
解决办法
5335
查看次数

如何从URL链接(深层链接)重新打开(或恢复)以前启动的活动?

我正在使用深层链接打开我的应用程序这是意图过滤器:

        <intent-filter>
            <data android:scheme="http" />
            <data android:scheme="https" />
            <data android:host="myapp.delivery" />
            <data android:pathPattern="/.*" />

            <action android:name="android.intent.action.VIEW" />

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
        </intent-filter>
Run Code Online (Sandbox Code Playgroud)

我们的用例是:

  1. 用户打开应用程序,浏览一些餐馆
  2. 用户点击餐厅(用餐馆菜单打开MenuActivity活动)
  3. 用户单击菜单类别,例如Pizzas或Desserts或Soups,这将打开CategoryActivity.
  4. 用户点击菜单添加到购物车我需要向用户发送带有网址的电子邮件.

我的问题:

(在步骤4之后)用户点击电子邮件应用程序(例如gmail或收件箱应用程序)上的URL,提示用户打开我的应用程序或浏览器,如下所示:

在此输入图像描述

当用户点击打开我的应用程序时,会发生GotEmailActivity在gmail应用程序之上打开,我需要它在我之前打开的应用程序实例之上打开.

正如你在这里看到的那样,我的应用程序的2个实例是打开的(应用程序的第一个实例,第二个实例在gmail之上打开),:

在此输入图像描述

如何在我以前推出的应用程序中打开点击链接,而不是在gmail/inbox应用程序之上?

android android-manifest android-intent android-lifecycle android-activity

3
推荐指数
1
解决办法
2589
查看次数

如何保持 Android 应用程序运行并退出 Unity Activity

我正在开发一个与 Unity3d 集成的应用程序,在我的代码的某些部分,我调用了一个 UnityPlayerActivity 来启动 Unity3d 代码。当用户完成使用 Unity3d 时,他按下一个按钮并返回到 Android 代码。我的问题是当我回到 android 代码时,在 UnityPlayerActivity 调用之后:

@Override protected void onDestroy ()
{
    mUnityPlayer.quit();
    super.onDestroy();
}
Run Code Online (Sandbox Code Playgroud)

应用程序关闭,不仅仅是Unity Activity。我不能调用这个方法,但我认为那会浪费内存。

有什么办法可以只退出 Unity3d 活动并保持我的应用程序正常运行,这样我就可以再次启动 Unity3d 部分吗?

塔克斯

android unity-game-engine android-lifecycle

3
推荐指数
1
解决办法
3784
查看次数

现在必须显式声明注释处理器。- compiler-1.1.1.jar (android.arch.lifecycle:compiler:1.1.1)

我正在尝试将以下库添加到我的应用程序中

   ext {
    roomLibraryVersion = '1.1.0'
    lifeCycleVersion = '1.1.1'
    }

//room data base
implementation "android.arch.persistence.room:runtime:$roomLibraryVersion"
implementation "android.arch.persistence.room:compiler:$roomLibraryVersion"
annotationProcessor "android.arch.persistence.room:compiler:$roomLibraryVersion"

//life cycle
implementation "android.arch.lifecycle:runtime:$lifeCycleVersion"
implementation "android.arch.lifecycle:compiler:$lifeCycleVersion"
implementation "android.arch.lifecycle:extensions:$lifeCycleVersion"


// rx android
implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
implementation "io.reactivex.rxjava2:rxjava:2.1.14"
implementation 'android.arch.persistence.room:rxjava2:1.1.0'

//paging
implementation 'android.arch.paging:runtime:1.0.0'
Run Code Online (Sandbox Code Playgroud)

我在运行代码时遇到异常。任何人都可以帮我解决这个问题

Execution failed for task ':app:javaPreCompileDebug'.
> Annotation processors must be explicitly declared now.  The following dependencies on the compile classpath are found to contain annotation processor.  Please add them to the annotationProcessor configuration.
    - compiler-1.1.1.jar (android.arch.lifecycle:compiler:1.1.1)

  Alternatively, set android.defaultConfig.javaCompileOptions.annotationProcessorOptions.includeCompileClasspath = …
Run Code Online (Sandbox Code Playgroud)

android android-lifecycle android-paging

3
推荐指数
1
解决办法
2198
查看次数

我是否需要在 onDestroy() 事件上调用 removeObserver 进行生命周期?

这是一个非常简单的问题:

背景

我正在使用相对较新的Lifecycle 类android 架构组件库的一部分)以Activity/Fragment更简单的方式处理一些事件。

这是你如何使用它来处理ON_DESTROY事件:

            lifecycle.addObserver(object : LifecycleObserver {
                @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
                fun onDestroy() {
                    lifecycle.removeObserver(this)
                    //Do something
                }
            })
Run Code Online (Sandbox Code Playgroud)

问题

我无法在文档和此处找到,是应该调用removeObserver还是在ON_DESTROY事件时自动完成。

我试过的

我试图阅读它,现在作为预防措施,我总是调用removeObserver.

问题

它是安全的避免调用removeObserverON_DESTROY事件?

android android-lifecycle android-architecture-lifecycle

3
推荐指数
1
解决办法
3079
查看次数

将 CameraX + CameraView 绑定到生命周期的问题

我正在尝试在干净的 android 应用程序中包含一个 CameraView 组件(来自 CameraX jetpack),如下所示:

https://medium.com/@hitherejoe/exploring-camerax-on-android-camera-view-daae6dfaa4ec https://developer.android.google.cn/jetpack/androidx/releases/camera

CameraView 非常简单,但将其绑定到 Activity 生命周期时出现问题。活动已授予相机权限。我试过在没有 CameraView 的情况下使用 CameraX(使用 SurfaceView + 所有设置),但以相同的结果和异常结束(并且 CameraView 使用起来要简单得多)。在三星 S10 和红米 Note 7 上测试过。 似乎组件尺寸/测量有问题:

例外

java.lang.BootstrapMethodError: Exception from call site #2 bootstrap method
        at androidx.camera.core.Preview.updateListener(Preview.java:368)
        at androidx.camera.core.Preview.updateOutput(Preview.java:586)
        at androidx.camera.core.Preview.updateConfigAndOutput(Preview.java:363)
        at androidx.camera.core.Preview.onSuggestedResolutionUpdated(Preview.java:524)
        at androidx.camera.core.UseCase.updateSuggestedResolution(UseCase.java:408)
        at androidx.camera.core.CameraX.calculateSuggestedResolutions(CameraX.java:684)
        at androidx.camera.core.CameraX.bindToLifecycle(CameraX.java:195)
        at androidx.camera.view.CameraXModule.bindToLifecycleAfterViewMeasured(CameraXModule.java:268)
        at androidx.camera.view.CameraView.onMeasure(CameraView.java:364)
        at android.view.View.measure(View.java:24973)
        at androidx.constraintlayout.widget.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1227)
        at androidx.constraintlayout.widget.ConstraintLayout.onMeasure(ConstraintLayout.java:1572)
        at android.view.View.measure(View.java:24973)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7139)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
        at android.view.View.measure(View.java:24973)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7139)
        at androidx.appcompat.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:403)
        at android.view.View.measure(View.java:24973)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7139)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185) …
Run Code Online (Sandbox Code Playgroud)

android android-lifecycle android-camera2 androidx android-camerax

3
推荐指数
1
解决办法
1285
查看次数

如何在 RecyclerView.Adapter 中获取 LifecycleOwner 引用?

目前,我用mLifecycleOwner = mContext as LifecycleOwnerget LifecycleOwner,它可以工作,但我认为它不是一个好的代码。

我怎样才能LifecycleOwnerListAdapter

class VoiceAdapters (private val aHomeViewModel: HomeViewModel, private val mPlay: PlayInterface):
        ListAdapter<MVoice, VoiceAdapters.VoiceViewHolder>(MVoiceDiffCallback()) {

    private lateinit var mContext: Context
    private lateinit var mLifecycleOwner:LifecycleOwner

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VoiceViewHolder {
        mContext = parent.context
        mLifecycleOwner = mContext as LifecycleOwner

        return VoiceViewHolder(
            LayoutVoiceItemBinding.inflate(LayoutInflater.from(parent.context), parent, false).also {               
               it.lifecycleOwner = mLifecycleOwner
               it.aHomeViewModel = aHomeViewModel
            }
        )
    }

    ...
}
Run Code Online (Sandbox Code Playgroud)

android android-lifecycle

3
推荐指数
1
解决办法
449
查看次数