简体   繁体   中英

Android: How to prevent Broadcast from being lost on orientation change?

My question is, if I rotate my device and a broadcast from the system for example from DownloadManager.ACTION_DOWNLOAD_COMEPLETE gets sent between onResume() / onPause() , what is the best practice to prevent that broadcast from getting lost? Thanks.

onResume(){
  registerReceiver(receiver, filter);
}

// rotate device now

onPause(){
  unregisterReceiver(receiver);
}

// broadcast gets sent now

Just add this line to your Manifest.xml

android:configChanges="orientation|screenSize"

<activity
        android:name=".YourActivity"
        android:label="@string/app_name"
        android:configChanges="orientation|screenSize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

</activity>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM