Answer the question
In order to leave comments, you need to log in
How to update the UI after the user has left the application in Android?
There is an IntentService that works in the background. After it completes, the result is sent as a BroadcastManager to the Main Activity, where it updates the UI of the Main activity.
But the problem is that when the user exits the application, the message (in the form of an Intent) is received by the BroadcastReceiver, but when the user returns to the application, he does not see this message, in the sense of the updated UI, because the onCreate method is launched , which represents the Activity in its original form, i.e. The UI is not updated even though the IntentService is working as it should.
How is it possible to save the current state of the Main Activity for the user and show the result after re-opening the application? MainActivity.java code:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageFromCamera = (ImageView) findViewById(R.id.imageView1);
ocrTextView = (EditText) findViewById(R.id.ocrText);
//set an Intent Filter
IntentFilter statusIntentFilter = new IntentFilter(ServiceBackground.Constants.BROADCAST_ACTION);
receiver = new ResponseReceiver();
LocalBroadcastManager.getInstance(this).registerReceiver(receiver,statusIntentFilter);
}
private class ResponseReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() != null && intent.getAction().equals(ServiceBackground.Constants.BROADCAST_ACTION)) {
System.out.println(intent.getAction());
final Bitmap imageRetrieved = intent.getParcelableExtra("bitmap");
String responseRetrieved = intent.getStringExtra("response");
imageFromCamera.setImageBitmap(imageRetrieved);
ocrTextView.setText(responseRetrieved);
}
}
}
public class ServiceBackground extends IntentService {
public ServiceBackground() {
super("My_Worker_Thread");
}
@Override
protected void onHandleIntent(Intent intent) {
Toast.makeText(this, "Service started", Toast.LENGTH_LONG).show();
Uri originalUri = intent.getData();
String absolutePathToImage = UriToPathActivity.getRealPathFromUri(getApplicationContext(),id);
final Bitmap bitmap = BitmapFactory.decodeFile(absolutePathToImage, bmOptions);
Intent localIntent = new Intent(Constants.BROADCAST_ACTION);
//bitmap responseReceivedполучены некоторой логикой
localIntent.putExtra("bitmap", bitmap);
localIntent.putExtra("response", responseReceived);
LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent);
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question