Close and open dialog while new activity ended - android

I have activity with dialog. Then I go to new activity where I do some operation and back to main activity. And I want to show dialog with refreshed data. So I did
#Override
protected void onRestart() {
super.onRestart();
dialog.dismiss();
dialog.show();
}
because if I close it and re-open it manualy the data will change. If I check it in debugger dialog shows, but in normal runtime not. What can I do to close and open it?

Try changing in onPause and onResume.
#Override
protected void onPause() {
super.onPause();
dialog.show();
}
And in onResume
#Override
protected void onResume() {
super.onResume();
dialog.dismiss();
}
OR
See onActivityResult

Solution 1: call dialog.dismiss(); in onStop() and dialog.show(); in onResume() of your activity.
Solution 2: fire broadcast from your second activity and receive it to your first. base on it update data.
Solution 3: start activityForResult and on result receive, update your dialog.

You can go to the new activity like:
Intent intent = new Intent();
startActivityForResult(intent, 0);
Then, to go back to the main activity passing the new data, you can use .putExtra:
Intent intent = new Intent();
//The object containing the updated data:
intent.putExtra("DATA", dataObject);
getActivity().setResult(200, intent);
getActivity().finish();
In the main activity, use onActivityResult to retrieve the data:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(data != null){
if(resultCode == 200 && data.hasExtra("DATA")){
DataClass dataObject = data.getParcelableExtra("DATA");
//Here you can use the new data to update the content of your dialog...
}
}
}
Hope it helps...

Related

Communication to activity in stack

Let's consider the following situation:
Activity A opens Activity B. Now, A is in activity stack. B downloads any data from the Internet and, basing on that data, we conclude that when user come back to the A ( after press back) A should refresh its content. How to say: B: Hey A in stack, please remember that you should refresh your content. I see that I can set some flag in App instance, but, it seems to be weird.
Consider using startActivityForResult in your ActivityA to call ActivityB, then within your ActivityB, override onBackPressed() method and call setResult() based on downloaded data. Finally back into your ActivityA override onActivityResult(int requestCode, int resultCode, Intent data)
Use the following example as guide:
ActivityA.java
public class ActivityA extends AppCompatActivity {
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this, ActivityB.class);
startActivityForResult(intent, 1234);
}
#Override
protected void onActivityResult(final int requestCode, final int resultCode, Intent intent) {
if (1234 == requestCode) {
if (resultCode == Activity.RESULT_OK) {
Toast.makeText(this, "Do action 1", Toast.LENGTH_SHORT).show();
}
if (resultCode == Activity.RESULT_CANCELED) {
Toast.makeText(this, "Do action 2", Toast.LENGTH_SHORT).show();
}
}
}
}
ActivityB.java
public class ActivityB extends AppCompatActivity {
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void onBackPressed() {
final Intent intent = new Intent();
if (true) { //Replace this condition by yours based on downloaded data
setResult(Activity.RESULT_OK, intent);
} else {
setResult(Activity.RESULT_CANCELED, intent);
}
super.onBackPressed();
}
}
Not that way. Always update Activity A content in the onResume() method of A activity.

Android startActivityForResult from AsyncTask with context

I have an activity, it's the sender. So when I push a button in this activity, it creates an AsyncTask, and when I get an answer from the server I have only Context from the sender activity.
Sender:
It starts AsynTask.
AsyncTask (mContext is context from Sender class):
Intent i = new Intent();
i.setClass(mContext, SecondActivity.class);
Sender act = (Sender)mContext;
act.startActivityForResult(i,1);
And in SecondActivity:
#Override
public void onPause()
{
super.onPause();
Intent returnIntent = new Intent();
returnIntent.putParcelableArrayListExtra("result", data);
setResult(RESULT_OK, returnIntent);
finish();
}
In the Sender, I try to get result:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_OK){
ArrayList<MyClass> result = data.getParcelableArrayListExtra("result");
addFriends(result);
}
}
}
But the requestCode is equal to 0 (when I am started it with 1) and resultCode = 0. I don't know why.
I hope someone can help me!
Thanks!
onPause method it`s not the good place to return the result to the activity. Try to return it in the another place - on some button action or in onBackPressed method.
In the 2nd activity use onBackPressed, and its works!
I dont know why dont work in onPause
#Override
public void onBackPressed()
{
Intent returnIntent = new Intent();
setResult(RESULT_OK, returnIntent);
finish();
super.onBackPressed();
}

onActivityResult not being called when launching another app

I am trying to call another app through intent, the app is called but the onActivityResult is not being called. Could someone please help me on this?
Below is my code:
public class EncryptCommandActivity extends Activity{
EncryptionFactory encryptionFactory = new EncryptionFactory();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.encrpyt_command_activity);
ActivityContexts.setEncryptCommandActivityContext(this);
Intent intent = new Intent("asd.com.qweapi.MAIN_ACTIVITY");
Bundle bundle = new Bundle();
bundle.putInt("Function", 1006);
bundle.putString("MSG", MQTTFactory.getById());
intent.putExtras(bundle);
startActivityForResult(intent, 0);
finish();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent pData)
{
super.onActivityResult(requestCode,resultCode,pData);
Log.d("Encrypt","Inside"); //not called
Toast.makeText(ActivityContexts.getMainActivityContext(),"encrypt", Toast.LENGTH_LONG).show(); //not called
}
}
You should remove finish() in onCreate() because it will finish activity and then it's no longer to exist, since can not fire onActivityResult()
move finish() to onActivityResult from onCreate

Make an activity A wait an activity B finish to continue

I have an activity A with this fuction:
public void onSettingsClick(View view){
Intent intent = new Intent(this, Settings.class);
startActivity(intent);
checkSettings();
}
Is there any way to make the activity A to wait for the activity B ("Settings.class") finish to execute the fuction "checkSettings();" ?
Thanks
Edit: I may have misunderstood your question. If you want to run checkSettings() function in B then you need to define and call that function in your B activity.
Otherwise, if you want to wait for activity B to end before running checkSettings() then copy the following code.
In A:
public void onSettingsClick(View view){
Intent intent = new Intent(this, Settings.class);
startActivityForResult(intent, 1);
}
then also in A Override onActivityResult method.. this gets called when B ends:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
checkSettings();
}
In your Activity A write
public void onSettingsClick(View view){
Intent intent = new Intent(this, Settings.class);
startActivityForResult(intent, 100);
}
and also in you Activity A override onActivityResult
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK){
if(requestCode==100){
checkSettings();
}
}
}
and in your Activity B
when you want to finish that activity write that piece of code there
Intent returnIntent = new Intent();
setResult(RESULT_CANCELED, returnIntent);
finish();
Another approach to doing this since you are launching a separate activity is call check settings in onResume. This way since you're not having to pass any data and if the are other reasons you may want to refresh or recheck your settings, it will do so any time the activity is brought back to the top.
override fun onResume() {
super.onResume()
checkSettings()
}

Show a button on a previous activity when going back from current activity

I want to visible a edit button on my previous activity by going back using back button. But when I am using
#Override
public void onClick(View arg0) {
// finish the current activity
finish();
}
on my current activity, it is going back to previous activity, but edit button doesn't become visible.
If I am using onResume on previous activity
#Override
protected void onResume() {
super.onResume();
btnEdit.setVisibility(View.VISIBLE);
}
then it's always visible, no matter if the activity is resuming or created for the first time.
I am new in Android development, please help me to solve this problem.
Ok,
When you start the Activity, instead of startActivity(intent) use startActivityForResult(intent, 1989) (The 1989 can be whatever int you want).
When you return from the new Activity, before calling finish() do the following:
Intent returnIntent = new Intent();
returnIntent.putExtra("resultBool", true);
//Null checks, not strictly neccescary
if (getParent() == null)
{
setResult(Activity.RESULT_OK, returnIntent);
}
else
{
getParent().setResult(Activity.RESULT_OK, returnIntent);
}
finish();
Then in your first Activity override onActivityResult() like so:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
// The int you initially used
if (requestCode == 1989)
{
if (resultCode == RESULT_OK)
{
boolean result = data.getBooleanExtra("resultBool", false)
if (result)
//Show the button now
}
}
}
You can make a boolean variable which can be set to true when you finish and make a check with this boolean in your onResume method
Override onRestart() method
#Override
protected void onRestart () {
super.onResume();
btnEdit.setVisibility(View.VISIBLE);
}
Or start Activity with
startActivityForResult (Intent intent, int requestCode)
method and override
#Override
protected void onActivityResult (int requestCode, int resultCode, Intent data){
super.onActivityResult (requestCode,resultCode,data);
btnEdit.setVisibility(View.VISIBLE);
}

Categories

Resources