- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
在Android应用程序中,我在MainActivity的onCreate中注册了一个接收器
IntentFilter mFilter = new IntentFilter("Action");
LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, mFilter);
在其 onResume 中
new Thread(new Runnable() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
Intent i = new Intent("Action");
LocalBroadcastManager.getInstance(MainActivity.this).sendBroadcast(i);
}
});
}
}).start();
坦白说,我不确定为什么我们要使用这样的线程(我从某个地方复制了代码,但没有完全消化它)。
此应用程序支持 ViewPager,因此在其关联的 Fragment 的 onCreate 中
IntentFilter mFilter = new IntentFilter("Action");
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mReceiver, mFilter);
在 MainActivity 和 Fragment 类中,接收器如下所示:
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
...
}
两个类中只有 onReceive 内部的内容不同。
我不太了解 LocalBroadcast 是如何工作的,我期望一旦发出广播,两个接收器处理程序都会运行。相反,我注意到大多数时候只有 MainActivity 中的接收器运行,偶尔 fragment 类中的接收器运行。
我的预感是与线程部分有关。
最佳答案
这种行为背后的原因可能是Activity 和 Fragment 的生命周期:
根据我的经验,当您有 Activity+Fragment 时,如何调用方法是:
onCreate()
onStart()
onResume()
onCreateView()
onStart()
onResume()
Explanation :
As your Fragment is not initialized yet when you are broadcasting from
onResume()
in Activity it could not be received at first by The Fragment but only receive by Activity. After that once the fragment is initialized the Broadcast will be received by the Fragment also.
关于java - android LocalBroadcast的两个接收者,一个在MainActivity中,另一个在fragment中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37759824/
因此,我正在学习 Lynda.com 上关于使用 Google map V2 构建移动应用程序的教程,并且我正在学习一个让您创建 map 应用程序的部分,该应用程序以编程方式查找位置,而不是依赖于se
我有一个 Mainactivity,其中包含一个 Layout,它是 4 个子布局的父级。单击子布局后,我将使用一个新 fragment 替换主布局。但我无法在按下返回按钮后返回 MainActivi
如你所见 this@MainActivity 或 MainActivity@this 在 kotlin 中开始 Activity 时有效。我试图找出答案,但我什么也没找到。谁能知道它的确切区别以及哪个
Solve by using getActivity() 我有这个 MainActivity.java 和 RepeatEntry.java 在我的 MainActivity 中,我有这个代码来实现
我正在尝试将 Google Analytics 集成到我的应用程序中,并遵循来自 here 的教程一切就绪: 我有一个 MainActivity,它扩展了 Activity 并附加了三个 Tab Fr
EditText 对象未创建,为什么? EditText editText=(EditText)findViewById(R.id.edittext); 这适用于扩展 AsyncTask 类的应用程序
所以我正在尝试了解 Android Fragments。如果我将以下代码放入我的 MainActivity 中: public void getMessage(Object obj) { Lo
我试图在我的Android设备上运行我的Android应用程序,但它一直显示我的主活动不存在,尽管我的主活动类在那里。我试图创建一个新的项目,然后复制我以前的代码,它在一段时间内奏效了。但随后它又产生
//主要活动 package com.going.books; import androidx.appcompat.app.AppCompatActivity; import androi
这似乎是一个我不好意思问的基本问题,但我对我的 Fragment 学习曲线感到非常沮丧,以至于我会暴露我的无知。 教科书中的一个例子,它削减了很多角落,使得扩展变得困难,即使它们有效,也没有按钮可以点
我正在将我的启动 Activity 从“MainActivity”更改为“RealMain”我在 list 中声明了这一点,但是 MainActivity 仍然首先出现。有人可以启发我吗?代码来 se
它失败了@ below line - bindService(intent, m_serviceConnection, Context.BIND_AUTO_CREATE); 下面是轨迹.... Act
我的项目在调试状态下正常工作。当我打开MainActivity.java文件时,这在import androidx.annotation.NonNull;上显示了问题 并在下面显示错误。 FAILUR
我在 MainActivity 类中看到了这样的代码: class MainActivity : AppCompatActivity() { private val languages = a
我的问题是关于如何制作启动屏幕,并在 MainActivity 完成渲染时完成启动屏幕,而不是通过设置超时。 我已经搜索过如何制作闪屏并且我已经做到了,但主要是他们使用超时来设置闪屏何时必须关闭/完成
大家好,我正在尝试为我的 Android 手机开发一个手电筒应用程序,但是当我运行它时出现错误。 GUI 有 2 个按钮:1 个用于开灯,另一个用于关灯 这是代码: package com.examp
如何解决这个问题? MainActivity.this is not an enclosing class. 谢谢 public class uploadToServer extends AsyncT
我的 logcat 中出现一些错误,并且我的应用程序无法在我的智能手机上运行...我更改了 list 和 MainActivity 中的一些代码(名为 HomaActivity ) 这是我的 list
我想知道使用单例 MainAcitivity 是否是一个糟糕的设计选择,如下所示: public class MainActivity extends AppCompatActivity ... {
我正在创建一个像 WhatsApp 一样的应用程序,但我遇到了问题。该应用程序应该进入 LoginActivity,但它直接进入 MainActivity,而无需我登录。 我尝试更改 Android
我是一名优秀的程序员,十分优秀!