JOHNCENA
JOHNCENA

Reputation: 99

how to pause parent activity until child activity finish

I have some files which i want to send through the Bluetooth. Here is my code

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ArrayList<Uri> uris = new ArrayList<Uri>();
        String multifile[] = {"/sdcard/file1.txt", "/sdcard/file2.txt", "/sdcard/file3.txt"};
        int len = multifile.length;
        Intent Int = new Intent();

        Int.setAction(android.content.Intent.ACTION_SEND_MULTIPLE);
        Int.setType("*/*");

        String packageName = null;
        String className = null;
        boolean found = false;
        PackageManager pm = getPackageManager();
        List appsList = pm.queryIntentActivities( Int, 0);

        ArrayList<ResolveInfo> list = (ArrayList<ResolveInfo>) pm.queryIntentActivities(Int, PackageManager.PERMISSION_GRANTED);
        for(ResolveInfo info: list) {
            packageName = info.activityInfo.packageName;

            if( packageName.equals("com.android.bluetooth")) {
                className = info.activityInfo.name;
                found = true;
                break;// found
            }
        }

        if (!found) {
            Toast.makeText(MainActivity.this, "not foud bluetooth",
                    Toast.LENGTH_SHORT).show();
            // exit
        }

        Int.setClassName(packageName, className);

        for(int i=0;i<len;i++) {
            File file=new File(multifile[i]);
            uris.add(Uri.fromFile(file));
        }

        Int.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
        startActivity(Int);
        onStop(); //stop here till all file have been sent 
        //delete all files which is to be send    
    }
}

all three files have been sent but the problem is execution of Mainactivity continue before Intent Int is finished due that all files are delete before sending the files Please help how to stop execution of Mainactivity until Int send all files

Upvotes: 1

Views: 2268

Answers (3)

vinothp
vinothp

Reputation: 10069

From your MainActivity call the ChildActivity by using startActivityForResult(). So the main activity will wait until the child activity finish its process. When the child activity get finished use setResult() to indicate the MainActivity that the process is completed.

Receive the result in onActivityResult() and then do what ever you want from the MainActivity.

Upvotes: 0

IanB
IanB

Reputation: 3489

You can use startActivityForResult. The parent activity will wait for a callback from the called activity.

e.g.

Intent i = new Intent(this,CalledActivity.class);
startActivityForResult(i, requestCode);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d("CheckStartActivity","onActivityResult and resultCode = "+resultCode);
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==1){
    Toast.makeText(this, "Pass", Toast.LENGTH_LONG).show();
}
else{
    Toast.makeText(this, "Fail", Toast.LENGTH_LONG).show();
}

Upvotes: 0

mah
mah

Reputation: 39837

If your "mainactivity" is starting the child, have it start it using startActivityForResult() and have it not take further action (not delete files) until the child result is available and indicates that it finished without errors.

If there isn't this parent/child relationship, there are several options but one easy one is have the lower level activity broadcast a message that the higher level activity receives and only does the follow-up work after it has been received.

Upvotes: 1

Related Questions