Start onActivityResult after the completion of loop

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Start onActivityResult after the completion of loop



I have a buttonClickListener that is intended to uninstall multiple apps so I've run a for loop for that and I want to start onActivityResult after the completion of all uninstallation process. SO far onActivityResult runs after each uninstallation. I want only one onActivityResult at the end. Is there any way I can do that?


buttonClickListener


onActivityResult


onActivityResult


onActivityResult


btnSelection.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
for (int i = 0; i < appList.size(); i++)
AllApps singleApp= appList.get(i);
if (singleApp.isSelected())
String app_pkg_name = singleApp.getPackageName();
Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE);
intent.setData(Uri.parse("package:" + app_pkg_name));
intent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
startActivityForResult(intent, UNINSTALL_REQUEST_CODE);



);



And this is the onActivityResult:


@Override
protected void onActivityResult(int requestCode, int resultCode, final Intent data)
super.onActivityResult(requestCode, resultCode, data);

if(requestCode==UNINSTALL_REQUEST_CODE)

//things to do






3 Answers
3



First , is not possible but you can just simply execute onActivityResult for the last command and in order to do that, you need to have a filtered list first


onActivityResult


btnSelection.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
// filter selected apps
List<AllApps> temp = new ArrayList<>();
for(AllApps app : appList)
if(app.isSelected())
temp.add(app);


// invoke app uninstallations
for (int i = 0; i < temp.size()-1; i++)
startActivity(uninstallAppIntent(temp.get(i)));

// invoke get result for last entry
startActivityForResult(uninstallAppIntent(temp.get(temp.size()-1)), UNINSTALL_REQUEST_CODE);

);

// to avoid code duplicity
Intent uninstallAppIntent(Allapps singleApp)
String app_pkg_name = singleApp.getPackageName();
Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE);
intent.setData(Uri.parse("package:" + app_pkg_name));
intent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
return intent;



One possible solution:



You can put variable i on data of intent and check if i==app.size() on onActivityResult.


i


i==app.size()


onActivityResult



Its A Simple use case of a If statement . you can take a boolean or just check appropriate condition. Checkout the code below .


If


boolean


btnSelection.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
AllApps singleApp=null;
for (int i = 0; i < appList.size(); i++)
if (appList.get(i).isSelected())
singleApp=appList.get(i);


if(singleApp!=null)
String app_pkg_name = singleApp.getPackageName();
Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE);
intent.setData(Uri.parse("package:" + app_pkg_name));
intent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
startActivityForResult(intent, UNINSTALL_REQUEST_CODE);


);



NOTE IF appList contains multiple selected items then the last indexed item will be used . In this case if you want first selected item the you should break the loop .


appList


break





you are not triggering uninstall for other apps
– Pavneet_Singh
Aug 10 at 4:24





I have provided the logic only . i don't think multiple apps can be uninstalled at once . So it should be done one by one .
– ADM
Aug 10 at 4:25






I think it would a good idea to add the logic to accomplish the task to keep only the last selected object for result
– Pavneet_Singh
Aug 10 at 4:28






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS