【问题标题】:startActivityForResult new activity immlidately finish make the screen the flashstartActivityForResult 新活动立即完成使屏幕闪烁
【发布时间】:2016-05-27 09:36:52
【问题描述】:

Activity A 启动 Activity B 获取一些数据。 B设置数据立即完成。这使 B 活动闪烁一次。有什么办法可以防止闪退?

TextToSpeech.java

Intent intent = new Intent(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
intent.setPackage(engine);
startActivityForResult(intent, VOICE_DATA_INTEGRITY_CHECK);

引擎活动获取requestCode,设置数据并完成。这使引擎活动显示 -> 设置数据 -> 完成。看起来像 Activity 的东西一闪而过。有什么办法可以防止这种情况发生吗?谢谢。

【问题讨论】:

  • 你可以使用intent和finish()...因为你可以展示你做了什么作为一个例子...
  • Is there any way to prevent the flash? 什么闪存?我在您的源代码中没有看到任何闪存
  • @GoInterface:在调用 finish() 之前发布一些延迟
  • 不是最清晰的问题,但由于我在复制上述内容时体验到屏幕“闪烁”,所以对此表示赞同。
  • 非常感谢,我找到了解决方法。不是很好,但对我有用。我会发布它。

标签: android startactivityforresult


【解决方案1】:

如果您要执行的操作不需要对用户界面做任何事情,您可以使用 Service/Content-Provide 而不是 Activity。

喜欢:

import android.app.Service;
import android.content.Intent;
import android.os.Binder;

import android.os.IBinder;

public class BackgroundService extends Service {


    private final IBinder mBinder = new LocalBinder();

    public class LocalBinder extends Binder {
        BackgroundService getService() {
            return BackgroundService.this;
        }
    }


    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }
}

从 Activity A 调用服务:

startService(new Intent(A.this,BackgroundService.class));

【讨论】:

    【解决方案2】:
    public void onCreate() {
        // startActivityForResult(intent, VOICE_DATA_INTEGRITY_CHECK);   
        new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    Activity activity = getActivity();
                    if (activity != null && !activity.isFinishing() && !activity.isDestroyed()) {
                        startActivityForResult(intent, VOICE_DATA_INTEGRITY_CHECK);
                    }
                }
            }, 500);
     }
    

    我使用延迟startActivityForResult而不是直接调用它,否则它会显示一个空片段并立即闪出。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-19
      • 2016-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-29
      • 1970-01-01
      相关资源
      最近更新 更多