I have read the previously posted questions and answers for 2 days and I've tried every variation suggested as well as setting my launchMode attribute to "standard" in my manifest.
I'm trying to pass data from my second activity back to my first activity after pressing a button. After I press the button, the first activity is launched but it doesn't go back to my onActivityResult() method. I can't figure out why this is happening.
Here's my code from activity 2:
Button btnAdd = (Button) findViewById(R.id.btnAdd);
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Check that message is printed out to LogCat
System.out.println("hello test1 Activity2");
EditText band = (EditText) findViewById(R.id.txtBand);
band.setFilters(new InputFilter[] {
new InputFilter.LengthFilter(9)
});
EditText album = (EditText) findViewById(R.id.txtAlbum);
album.setFilters(new InputFilter[] {
new InputFilter.LengthFilter(9)
});
final Spinner genre = (Spinner) findViewById(R.id.spin_genre);
TextView selection = (TextView)genre.getSelectedView();
CharSequence strBand = band.getText();
CharSequence strAlbum = album.getText();
CharSequence strGenre = selection.getText();
//Check that we got input values
System.out.println("hello test Activity2- " +
strBand + " - " + strAlbum + " - " + strGenre);
//**********Intent Declaration************
Intent i = new Intent(getApplicationContext(), Activity1.class);
i.putExtra("strBand", strBand);
i.putExtra("strAlbum", strAlbum);
i.putExtra("strGenre", strGenre);
startActivityForResult(i, 0);
setResult(RESULT_OK, i);
finish();
}
});
Here's activity 1:
public class Activity1 extends Activity {
/** Called when the activity is first created. */
#SuppressWarnings("deprecation")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button addAlbum = (Button) findViewById(R.id.btnMain);
addAlbum.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent();
i.setClassName("jorge.jorge.jorge",
"jorge.jorge.jorge.Activity2");
startActivity(i);
}
});
}// end of onCreate()
//******************Callback Method****************************
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
//Checks if we got to ActivityResult
System.out.println("hello test 2: We got to Activity1");
if (resultCode == RESULT_OK)
{
Bundle returndata = data.getExtras();
String strAlbum = returndata.getString("strAlbum");
String strBand = returndata.getString("strBand");
String strGenre = returndata.getString("strGenre");
// check to see if we got the variable values from activity2
System.out.println("hello test 2: We got to Activity1 with variables - "
+ strBand + " - " + strAlbum + " - " + strGenre);
//Create table layout to contains views with variable values
TableLayout table = new TableLayout(this);
table.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//creates row with parameters
TableRow row = new TableRow(this);
row.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
//text views to contain variable values
TextView tv1 = new TextView(this);
tv1.setText(strBand);
row.addView(tv1);
TextView tv2 = new TextView(this);
tv2.setText(strAlbum);
row.addView(tv2);
TextView tv3 = new TextView(this);
tv3.setText(strGenre);
row.addView(tv3);
//adds the table row to the table layout
table.addView(row);
}
}// end onActivityResult()
}
I'm not sure if my activity callbacks are not placed properly in the code or if I'm not firing the intent properly or if I'm not setting up the callback with the right method or what. I know this topics has been discussed but I'm out of ideas. Thanks.
You've just got it backwards. If Activity1 is supposed to startActivity2 and Activity2 is supposed to send the result back to Activity1, you need to do it like this:
in Activity1:
Intent i = new Intent();
i.setClassName("jorge.jorge.jorge", "jorge.jorge.jorge.Activity2");
startActivityForResult(i); // This starts Activity2 and waits for the result
in Activity2:
Intent i = new Intent(getApplicationContext(), Activity1.class);
i.putExtra("strBand", strBand);
i.putExtra("strAlbum", strAlbum);
i.putExtra("strGenre", strGenre);
setResult(RESULT_OK, i);
finish(); // This closes Activity2 and generates the callback to Activity.onActivityResult()
Related
I have a function that generates a specific number of TextViews dynamically.
TextView tv;
EditText et;
public TextView textViewGenerate(final Activity activity, String tag, Integer id) {
tv = new TextView(activity);
GradientDrawable gd = new GradientDrawable();
gd.setColor(0xFFFFFF);
gd.setCornerRadius(4);
gd.setStroke(1, 0xFF757575);
tv.setBackground(gd);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT,
1.0f
);
tv.setPadding(7, 9, 0, 0);
tv.setGravity(Gravity.START);
tv.setTextSize(22);
tv.setTag(tag);
tv.setId(id);
tv.setHint("Enter Module Serial Number");
//Click to launch camera
tv.setClickable(true);
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Intent intent = new Intent(ct, MctCameraActivity.class);
Intent intent = new Intent(activity, MctCameraActivity.class);
activity.startActivityForResult(intent, 1);
//ct.startActivity(intent);
//ToDo: finish receiving the data from the activityForResult
}
});
lp.setMargins(10, 0, 10, 0);
lp.setMarginStart(10);
lp.setMarginEnd(10);
tv.setLayoutParams(lp);
return tv;
}
}
The onClick launches another activity that loads the camera in order to capture barcode data. In the main activity I create the TextViews with
final LayoutElements le = new LayoutElements();
mainLayout = (LinearLayout)findViewById(R.id.mctScanPageMain);
for(int i = 0; i < 3; i++) {
mainLayout.addView(le.textViewGenerate(this, "NewID" + i, i));
}
The onActivityResult returns the value from the camera activity but the issue is that it sets every generated TextView to have the same barcode number (the barcode number is returned as a string). How can I modify this so that only the text of the selected TextView gets changed in the onActivityResult?
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1 && resultCode == RESULT_OK && data != null) {
String returnResult = data.getStringExtra("result");
for (int i=0; i < mainLayout.getChildCount(); i++){
TextView tv = (TextView) mainLayout.findViewWithTag("NewID"+i);
if(tv == null) {
break;
} else {
tv.setText(returnResult);
}
}
}
}
In your click listener of TextView you can change:
Intent intent = new Intent(activity, MctCameraActivity.class);
activity.startActivityForResult(intent, 1);
to
Intent intent = new Intent(activity, MctCameraActivity.class);
intent.putExtra("text_view_tag", tag);
activity.startActivityForResult(intent, 1);
And then sent it back in result of MctCameraActivity, so you can use it in onActivityResult with getStringExtra. This way you will know tag of TextView which started activity.
Alternatively you can use requestCode that you pass to startActivityForResult to differentiate between text views. For example pass request code 1 for text view 1, request code 2 for text view 2 etc and use this requestCode in onActivityResult.
I have to put intent to a service from on click of a button, where the whole layout is created programmatically.
I have checked all answers for 'Android content activity not found exception' and have tried all answers. But still the problem persists. I have used try catch block, still the problem exists.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
db = new DBOperation(this);
document = getIntent().getStringExtra("document_id");
getAlldataList=db.getPartiesReport(db);
LinearLayout rootView = new LinearLayout(this);
LinearLayout rootView1 = new LinearLayout(this);
// RelativeLayout rootView2 = new RelativeLayout(this);
Button update1 = new Button(this);
update1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
update1.setGravity(Gravity.CENTER);
update1.setText("UPDATE");
update1.setBackgroundColor(Color.parseColor("#ffa500"));
update1.setTextColor(Color.WHITE);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) update1.getLayoutParams();
params.setMargins(20, 20, 20, 10);
update1.setLayoutParams(params);
update1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i < poslist.size(); i++) {
//
// SQLiteDatabase base3 = db.getWritableDatabase();
// base3.delete(UPDATEPARTIES1, null, null);
// System.out.println("Parties DB Deleted");
SQLiteDatabase db7 = db.getWritableDatabase();
ContentValues values5= new ContentValues();
values5.put(DBManager.TableInfo.KEYID, ID1);
values5.put(DBManager.TableInfo.DOCUMENT, document);
values5.put(DBManager.TableInfo.ATTENDANCE,attendancelist.get(i));
values5.put(DBManager.TableInfo.EMAILNEW, emaillist.get(i));
values5.put(DBManager.TableInfo.PARTYTYPE,partytypelist.get(i) );
values5.put(DBManager.TableInfo.BIOMETRIC,biometriclist.get(i));
values5.put(DBManager.TableInfo.KEY_LOGIN_USER,username2);
String condition5 = DBManager.TableInfo.DOCUMENT + " =?";
Cursor cursor5 = db7.query(DBManager.TableInfo.UPDATEPARTIES1, null, condition5, new String[]{DBManager.TableInfo.ATTENDANCE}, null, null, null);
long status5 = db7.insert(DBManager.TableInfo.UPDATEPARTIES1, null, values5);
System.out.println( "Parties insert : " + status5);
cursor5.close();
db7.close();
}
itemlist.clear();
poslist.clear();
IDlist.clear();
attendancelist.clear();
partytypelist.clear();
emaillist.clear();
biometriclist.clear();
System.out.println("Poslist:" + poslist.size());
System.out.println("itemlist:" + itemlist.size());
System.out.println("IDlist:" + IDlist.size());
Intent i1=new Intent(anulom.executioner5.com3.anulom.newstatusinfo.this,SendPartyReport.class);
startActivity(i1);
finish();
}
});
What I need is to do put a intent to a service SENDPARTYREPORT from the onclick listener of button UPDATE 1.
you have to call
startService(i1);
instead of
startActivity(i1);
The exception occurs because you tried to start an Activity and not a Service. If i unterstand you correct that the SendPartyReport is a Service then you will have to use the startService method.
Greetings
I have menu Menu 1 containing 20 buttons bt1, bt2,..btn when you click a button should go to the main activity Main1 where a title and a text should be displayed. Main1 is extended to Text1 where the titles and texts are.
What's needed is that when you click on button2 for example the Main1 should display Title2 and Text2 and so on.
I did this in the Menu 1:
#Override
public void onClick(View v) {
//do common code here
Bundle bundle1 = new Bundle();
bundle1.putString("somekey1", act1);
Intent i = new Intent(getApplicationContext(), Main1.class);
i.putExtras(bundle1);
startActivity(i);
str= v.getResources().getResourceName(v.getId());
act1= Integer.toString(Integer.parseInt(str.substring(str.indexOf("bt")+2 )));
in the Main1 I did this:
num = Integer.parseInt(getIntent().getExtras().getString("somekey1"));
stringId1 = getResources().getIdentifier("title"+num, "string", getPackageName());
stringId2 = getResources().getIdentifier("text"+num, "string", getPackageName());
if (stringId1 > 0) {
title=getString(stringId1);
text2=getString(stringId2);
}
in the Text1 I did this:
public class Text extends Activity {
public String
title1="some title",
text1="some text",
title2="some title",
text2="some text",
titl3="333",
tex3="kar3",
title4="xxxxx",
text4="xxxxx",
title5="",
text5="",
But all of that doesn't work, and about to shake my head on the wall, as the bundle doesn't transfer the data, and stringId1 = getResources().getIdentifier("title"+num, "string", getPackageName());
also returns 0.
Help please`
Pass the intent as shown below:
Intent intent = new Intent(getApplicationContext(),Main1.class);
intent.putExtra("Key1", "Value1");
intent.putExtra("Key2", "Value2");
startActivity(intent);
And in the oncreate() of your Main1.class :
Intent dataIntent = getIntent();
String value1 = dataIntent.getStringExtra("Key1");
String value2 = dataIntent.getStringExtra("Key2");
Please try the following
#Override
public void onClick(View v) {
//do common code here
Bundle bundle1 = new Bundle();
str= v.getResources().getResourceName(v.getId());
act1= Integer.toString(Integer.parseInt(str.substring(str.indexOf("bt")+2 )));
bundle1.putString("somekey1", act1);
Intent i = new Intent(getApplicationContext(), Main1.class);
i.putExtras(bundle1);
startActivity(i);
I am trying to modify an Android app to suit my need.
The original app has page 1 to display list of notes, and page 2 displays the detailed note.
What I'm trying to achieve is instead of having only 1 textbox in the detailed note page, I want it to have several textboxes, and persist it as well.
Here is how I thought it would be (but failed miserably of course).
This the transmitter on the page 2 (detailed page) activity:
private void saveAndFinish()
{
EditText et = (EditText) findViewById(R.id.eventTitle);
String eventTitle = et.getText().toString();
EditText et2 = (EditText) findViewById(R.id.eventDate);
String eventDate = et2.getText().toString();
EditText et3 = (EditText) findViewById(R.id.eventVenue);
String eventVenue = et3.getText().toString();
EditText et4 = (EditText) findViewById(R.id.eventLocation);
String eventLocation = et4.getText().toString();
EditText et5 = (EditText) findViewById(R.id.eventNote);
String eventNote = et5.getText().toString();
EditText et6 = (EditText) findViewById(R.id.eventAttendees);
String eventAttendees = et6.getText().toString();
Intent intent = new Intent();
// pass these to the main activity will ya?
intent.putExtra("key", data.getKey());
intent.putExtra("title", eventTitle); // eventTitle is the edited text!
intent.putExtra("date", eventDate);
intent.putExtra("venue", eventVenue);
intent.putExtra("location", eventLocation);
intent.putExtra("note", eventNote);
intent.putExtra("attendees", eventAttendees);
setResult(RESULT_OK, intent);
// work done, go back to calling activity
finish();
}
This the receiver on the page 1 (main page) activity:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode==DETAIL_ACTIVITY_REQ && resultCode==RESULT_OK)
{
DataItem event = new DataItem();
event.setKey(data.getStringExtra("key"));
event.setTitle(data.getStringExtra("title"));
event.setDate(data.getStringExtra("date"));
event.setVenue(data.getStringExtra("venue"));
event.setLocation(data.getStringExtra("location"));
event.setNote(data.getStringExtra("note"));
event.setAttendees(data.getStringExtra("attendees"));
datasource.update(event);
refreshDisplay();
}
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
// which list view item was selected?
DataItem data = eventsList.get(position);
//now, to navigate. which class should i go to next?
Intent intent = new Intent(this, DetailedActivity.class);
// and also, pass data to the next activity will ya?
intent.putExtra("key", data.getKey());
intent.putExtra("title", data.getTitle());
intent.putExtra("date", data.getDate());
intent.putExtra("venue", data.getVenue());
intent.putExtra("location", data.getLocation());
intent.putExtra("note", data.getNote());
intent.putExtra("attendees", data.getAttendees());
// go go go!
startActivityForResult(intent, DETAIL_ACTIVITY_REQ);
}
Apparently only the "title" is saved, everything else is not. Help?
modify your code in saveAndFinish method
Intent intent = new Intent(thisActivityname.this,secondActivityname.class);
startActivity(intent);
Above code is put data and send to second activity.
now get above data in second activity
Intent intent = getIntent();
String title = intent.getStringExtra("title");
String date = intent.getStringExtra("date");
String venue = intent.getStringExtra("venue");
String location = intent.getStringExtra("location");
String note = intent.getStringExtra("note");
String attendees = intent.getStringExtra("attendees");
You must use getIntent() to get the data from Intent:
data = getIntent();
use on ActivityResult:
data = getIntent();
I've a mapview that displays an itemized overlay, and each onTap event shows a dialog with information about travel agency and three buttons, one of them is supposed to open the agency web site in the browser, but when i click the button i got : no activity found to handle Intent.
here's my code :
protected boolean onTap(int i){
float[] results = new float[1];
Location.distanceBetween(decdegrees(appState.getMyLocation().getLatitudeE6()), decdegrees(appState.getMyLocation().getLongitudeE6()), decdegrees(points.get(i).getPoint().getLatitudeE6()), decdegrees(points.get(i).getPoint().getLongitudeE6()), results);
float distance = results[0]/1000;
DecimalFormat maxDigitsFormatter = new DecimalFormat("#.#");
String infos=points.get(i).getSnippet() + "#" + String.valueOf(maxDigitsFormatter.format(distance));
final String [] AInfos = infos.split("#");
final Dialog ADialog = new Dialog(c);
ADialog.setContentView(R.layout.travelagency);
TextView txtAgence = (TextView)ADialog.findViewById(R.id.txtAgence);
TextView txtAddress = (TextView)ADialog.findViewById(R.id.txtAddress);
TextView txtDistance = (TextView)ADialog.findViewById(R.id.txtDistance);
TextView txtFax = (TextView)ADialog.findViewById(R.id.txtFax);
Button btnCall = (Button)ADialog.findViewById(R.id.btnCall);
Button btnWebSite = (Button)ADialog.findViewById(R.id.btnWebSite);
Button btnCancel = (Button)ADialog.findViewById(R.id.btnCancel);
ADialog.setTitle(AInfos[0]);
btnCall.setText("Appeler : " + AInfos[2]);
txtAgence.setText(AInfos[1]);
txtDistance.setText("Approximativement à : " +AInfos[6] + " Km");
txtAddress.setText("Adresse : " + AInfos[3]);
txtFax.setText("Fax : " + AInfos[4]);
ADialog.show();
btnCancel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
ADialog.dismiss();
}
});
btnWebSite.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(Intent.ACTION_VIEW,Uri.parse(AInfos[5]));
v.getContext().startActivity(myIntent);
}
});
return (true);
}
I found examples here and here but suddently not work for me..
thanks
This will create the proper intent to open a web page in the default browser:
Uri url = Uri.parse("http://www.someUrl.com");
Intent intent = new Intent(Intent.ACTION_VIEW, url);
startActivity(intent);
Most probably your AInfos[5] is not a proper url. Hard-code a url like http://www.google.com and see if it works first. Also print what AInfos[5] contains.