Picture 1. This is my create_layout.
Picture 2. This is my custom contact list when I clicked on add member button.
Picture 3. NOW HERE IS THE PROBLEM. When press on the select button. I wanted to list the chosen contact value back to my first image layout. But it is opening a duplicate of my first layout and appearing there.
Here is my code.
#Override
public void onClick(View v) {
StringBuilder checkedcontacts= new StringBuilder();
System.out.println("............"+ma.mCheckStates.size());
for(int i = 0; i < name1.size(); i++)
{
if(ma.mCheckStates.get(i)==true)
{
checkedcontacts.append(name1.get(i).toString());
checkedcontacts.append("\n");
}
else
{
System.out.println("..Not Checked......"+name1.get(i).toString());
}
}
Intent i = new Intent (getApplicationContext(), CreateTab.class);
i.putExtra("str",checkedcontacts.toString());
startActivity(i);
finish();
}
});
I know the problem is that I make an intent so that when user clicks on select button, it will point back to CreateTab class which will repeat the onCreate. But how can I prevent from the onCreate again?
you should you use onActivityResult() method to read the returned result.
Do this in your CreateTab.class
Intent i = new Intent(getApplicationContext(), AddMember.class);
startActivityForResult(i, 100); // 100 is some code to identify the returning result
Method to read the result from newly created activity
#Override
protected void onActivityResult(int requestCode,
int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == 100){
String str = data.getExtras().get("str");
}
}
Send result back to old activity when StartActivityForResult() is used
Intent i = new Intent();
i.putExtra("str", checkedcontacts.toString());
// Setting resultCode to 100 to identify on old activity
setResult(100,i);
and close your AddMember activity
finish()
so your click event should look like this;
#Override
public void onClick(View v) {
StringBuilder checkedcontacts= new StringBuilder();
System.out.println("............"+ma.mCheckStates.size());
for(int i = 0; i < name1.size(); i++)
{
if(ma.mCheckStates.get(i)==true)
{
checkedcontacts.append(name1.get(i).toString());
checkedcontacts.append("\n");
}
else
{
System.out.println("..Not Checked......"+name1.get(i).toString());
}
}
Intent i = new Intent();
i.putExtra("str", checkedcontacts.toString());
// Setting resultCode to 100 to identify on old activity
setResult(100,i);
finish();
}
});
http://developer.android.com/training/basics/intents/result.html
Don't start previous activity again, just update selector Activity's result, and call finish().
Intent resultIntent = new Intent(this, PreviousActivity.class);
resultIntent.putExtra("selection",checkedcontacts.toString());
setResult(RESULT_OK,resultIntent);
finish();
Related
I'm trying to use onActivityResult to send a title, I've looked at the google implementation for the same task but it doesn't work me. Can someone help?
code for onActivityResult in mainActivity.
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
System.out.println("There is something coming to this function" + requestCode);
if(requestCode == NEW_TITLE_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK){
Title title = new Title(data.getStringExtra(notesSection.EXTRA_REPLY));
mTitleViewModel.insert(title);
}else{
Toast.makeText(getApplicationContext(), R.string.empty_not_saved, Toast.LENGTH_LONG).show();
}
}
code in my notesSection activity.
finishFab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// System.out.println("Button Has Been Clicked From Notes Section");
Intent replyIntent = new Intent();
if (TextUtils.isEmpty(titleEdit.getText())){
// System.out.println("Empty?");
setResult(RESULT_CANCELED, replyIntent);
}else{
// System.out.println("Sending Something Supposedly");
String title = titleEdit.getText().toString();
// System.out.println("Sending " + title);
replyIntent.putExtra(EXTRA_REPLY, title);
setResult(RESULT_OK, replyIntent);
}
finish();
// startActivity(new Intent(notesSection.this, MainActivity.class));
}
});
FYI: When I print something in the onActivityResult function, nothing shows up on my run terminal, I don't know why this is, but I don't think the function is being reached for some reason. I will send more code if necessary.
Thanks.
In your first activity try starting second activity like this
static int NEW_TITLE_ACTIVITY_REQUEST_CODE = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
---
---
---
// i assume you are starting activity on some button click
// so add following line in you button on click event
startActivityForResult(new Intent(MainActivity.this,notesSection.class),NEW_TITLE_ACTIVITY_REQUEST_CODE);
}
then override following method in your FIRST ACTIVITY
#Override
public void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
if (resultCode == Activity.RESULT_OK && requestCode == NEW_TITLE_ACTIVITY_REQUEST_CODE)
{
// do whatever you want
}
}
Also update your finish fab on click listener
finishFab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent replyIntent = new Intent();
if (TextUtils.isEmpty(titleEdit.getText())){
setResult(RESULT_CANCELED, replyIntent);
}
else{
replyIntent.putExtra("TITLE", titleEdit.getText().toString());
setResult(RESULT_OK, replyIntent);
}
finish();
}
});
Actually, this procedure has changed recently. You might want to take a look at the official documentation so that you can implement it according to your needs.
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?
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
}
}
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
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.
Its A Simple use case of a If statement . you can take a boolean or just check appropriate condition. Checkout the code below .
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 .
i have button which have attribute android:onClick="atnDuom".
There is that function
public void atnDuom(View view)
{
finish();
}
and there is onActivityResult function in the same activity.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
DOP = new DatabaseOperations(ctx);
Intent returnIntent = new Intent();
user_name = data.getStringExtra("tarpVard");
user_lastname = data.getStringExtra("tarpPav");
institucijos_pavadinimas = data.getStringExtra("tarpInst");
padalinio_pavadinimas = data.getStringExtra("tarpPad");
pareigos = data.getStringExtra("tarpPar");
mob_tel = data.getStringExtra("tarpMob");
el_pastas = data.getStringExtra("tarpEl");
setResult(RESULT_OK,returnIntent);
DOP = new DatabaseOperations(ctx);
if(newVard.equals("")||newPav.equals("")||newInst.equals("")||newPad.equals("")||newPar.equals("")||newMob.equals("")||newEl.equals(""))
{
Toast.makeText(getBaseContext(), R.string.prashome, Toast.LENGTH_LONG).show();
}
else
{
DOP.updateUserInfo(DOP, user_name, user_lastname, institucijos_pavadinimas, padalinio_pavadinimas, pareigos, mob_tel, el_pastas, newVard, newPav, newInst, newPad, newPar, newMob, newEl);
Toast.makeText(getBaseContext(), "Duomenys atnaujinti", Toast.LENGTH_LONG).show();
finish();
}
}
}
}
It is possible to execute function onActivityResult whithout doing anything in atnDuom function?
Finish() close activity and onActivityResult doesnt work :)
You are using data from the intent, if you want to go to onActivityResult from atnDuom you will need to create a new Intent and push all the data needed
Intent newIntent = new Intent();
newIntent.putExtras(...);
onActivityResult(REQUEST_CODE, RESULT_OK, newIntent);
i am not understanding what is going on in this piece of code. Need help please.
Intent i = new Intent(getApplicationContext(),
RSSNewsReaderPBActivity.class);
// send result code 100 to notify about product update
setResult(100, i);
startActivity(i);
and why use int value in it what it do.
This is method of the code
protected String doInBackground(String... args) {
String url = args[0];
rssFeed = rssParser.getRSSFeed(url);
Log.d("rssFeed", " " + rssFeed);
if (rssFeed != null) {
Log.e("RSS URL",
rssFeed.getTitle() + "" + rssFeed.getLink() + ""
+ rssFeed.getDescription() + ""
+ rssFeed.getLanguage());
RSSDatabaseHandler rssDb = new RSSDatabaseHandler(
getApplicationContext());
WebSite site = new WebSite(rssFeed.getTitle(),
rssFeed.getLink(), rssFeed.getRSSLink(),
rssFeed.getDescription());
rssDb.addSite(site);
Intent i = new Intent(getApplicationContext(),
RSSNewsReaderPBActivity.class);
// send result code 100 to notify about product update
setResult(100, i);
startActivity(i);
return null;
} else {
runOnUiThread(new Runnable() {
public void run() {
textViewMessage
.setText("Rss url not found. Please check the url or try again");
}
});
}
return null;
}
In this case, the setResult is wrongly used : setResult must be called before finishing an activity, it will inform the parent activity that your current one has worked well or if it had a problem (considering the parent activity has started the new one with startActivityForResult)
Let's say you have an ActivityOne that will launch an ActivityTwo:
ActivityOne:
Intent intent = new Intent(this, ActivityTwo.class);
startActivityForResult(intent, MY_REQUEST_CODE);
in ActivityTwo at the moment you want to close the activity:
if(everything_is_ok)
setResult(1);
else
setResult(0);
finish();
In ActivityOne, you will get notified that ActivityTwo has been finished:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode == MY_REQUEST_CODE)
{
if(resultCode == 1)
//everything went fine
else
//something went wrong
}
}
Hope this helps!
I have activity A where it has a ADD button when I click on the button it displays the list of users with check boxes in Second Activity and when I select the users and click the conform button it should display in all the users Activity A.I am using adapter to display the users.
from this button I am getting the list of checked values
private void checkButtonClick()
{
conform = (Button)findViewById(R.id.Confirm);
conform.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
StringBuffer responseText = new StringBuffer();
responseText.append("The following were selected...\n");
ArrayList<Namelist> stateList = contactadapteruser.listexample;
Bundle b=null;
Namelist state;
for(int i=0;i<stateList.size();i++)
{
state = stateList.get(i);
b = new Bundle();
b.putString("selected",state.getName());
if(state.isSelected())
{
responseText.append("\n" + state.getName());
}
}
Intent i= new Intent(Contactselectuser.this,Contactrepresentativedetails.class);
i.putExtras(b);
startActivity(i);
}
});
}
from this button I am getting the I am retrieving the values
send.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i2 = getIntent();
Bundle b1 = i2.getExtras();
String name1 = b1.getString("selected");
tweet.setText(name1+",\t");
}
});
My problem I am unable to get the users which are checked.
Can any one help me .
use start activity for result to start your second activity.
Intent intent = new Intent(this, SecondActivity.class);
startActivityForResult(intent, 1);
from second activity before closing you can send
Intent return_intent = new Intent();
return_intent.putExtra("your_user_list",user_list);
setResult(RESULT_CODE,return_intent);
finish();
Now in your FirstActivity override onActivityResult() method
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_CODE){
// GET YOUR USER LIST HERE AND USE IT FOR YOUR PURPOSE.
user_list=data.getExtra("your_user_list");
}
}
}
You can use broadcast sender and receiver also for your case. But above method is good for your task.
ArrayList<Namelist> stateList = contactadapteruser.listexample;
// convert your ArrayList to a String[]
String[] arr = new String[stateList.size()];
for (int i = 0; i < arr.length; i++)
{
arr[i] = stateList.get(i).getName();
}
Then using this question as a source, put the String array in your intent:
Bundle b = new Bundle();
b.putStringArray("selected", arr);
Intent i = new Intent(Contactselectuser.this,Contactrepresentativedetails.class);
i.putExtras(b);
startActivity(i);
Then to read the String array from your other activity:
Bundle b = getIntent().getExtras();
String[] array = b.getStringArray("selected");