Multiple startActivityForResult() - java

I need to call this function twice in my application pointing to two different activities. I have a unique request code for each call, however my app seems to crash everytime it launches the second activity.
Here is my code (only relevant parts):
MainActivity:
//Request Info vars
static final int GET_DETAILS = 1;
static final int EDIT_DETAILS = 2;
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
public void onMapClick(LatLng latLng) {
lat = latLng.latitude;
lon = latLng.longitude;
startActivityForResult(new Intent(MapsActivity.this,NewMarkerActivity.class), GET_DETAILS);
}
});
mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
current_marker = marker;
startActivityForResult(new Intent(MapsActivity.this,EditMarkerActivity.class), EDIT_DETAILS);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == GET_DETAILS) {
if (resultCode == RESULT_OK) {
String marker_title=data.getStringExtra("title");
String marker_snippet = data.getStringExtra("snippet");
addMarker(lat, lon, marker_title, marker_snippet);
mMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(lat, lon)));
}
} if (requestCode == EDIT_DETAILS) {
if (resultCode == RESULT_OK) {
String marker_title=data.getStringExtra("title");
String marker_snippet = data.getStringExtra("snippet");
current_marker.setTitle(marker_title);
current_marker.setSnippet(marker_snippet);
}
}
}
EditMarkerActivity:
public class EditMarkerActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_marker_activity);
Button save_btn = (Button)findViewById(R.id.btn_save);
save_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
EditText editName = (EditText)findViewById(R.id.editName);
String marker_title = editName.getText().toString();
EditText editSnippet = (EditText)findViewById(R.id.editSnippet);
String marker_snippet = editSnippet.getText().toString();
Intent _result = new Intent();
_result.putExtra("title", marker_title);
_result.putExtra("snippet", marker_snippet);
setResult(Activity.RESULT_OK, _result);
finish();
}
});
}
}
NewMarkerActivity:
public class NewMarkerActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_marker_activity);
Button save_btn = (Button)findViewById(R.id.btn_save);
save_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
EditText editName = (EditText)findViewById(R.id.editName);
String marker_title = editName.getText().toString();
EditText editSnippet = (EditText)findViewById(R.id.editSnippet);
String marker_snippet = editSnippet.getText().toString();
Intent _result = new Intent();
_result.putExtra("title", marker_title);
_result.putExtra("snippet", marker_snippet);
setResult(Activity.RESULT_OK, _result);
finish();
}
});
}
}
Any obvious issues? Help or insight to this problem will be greatly appreciated :)
Here is my output from Logcat:
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.geekybrackets.virtualtourguide/com.geekybrackets.virtualtourguide.EditMarkerActivity}; have you declared this activity in your AndroidManifest.xml?

Turns out the issue was that I hadn't defined the activity in the manifest file.
Dear old me, now i know to check my errors !
Thanks again guys, case closed.

Related

Transferring data from the third activity to the first

From the first activity I would like to go to the second, then from the second to the third. In the third activity I would like to enter the name in EditText, then after pressing the button, go to the first activity and at the same time send the information entered in the third activity.
Unfortunately, after pressing the button in the third activity, instead of returning to the first activity, I return to the second activity. Was the first activity killed? What could I do to ensure that the information is correct for the first activity? This is my code:
First:
public class MainActivity extends AppCompatActivity {
TextView textViewInformation;
Button button_GoToSecond;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewInformation = findViewById(R.id.textView);
button_GoToSecond = findViewById(R.id.button);
button_GoToSecond.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, Second.class);
startActivity(i);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent i) {
if((requestCode == 1) &&(resultCode == RESULT_OK)) {
String name = i.getStringExtra("name");
textViewInformation.setText(name);
}
}
}
Second:
public class Second extends AppCompatActivity {
Button button_GoToThird;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
button_GoToThird = findViewById(R.id.button2);
button_GoToThird.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(Second.this, Third.class);
startActivity(i);
}
});
}
}
Third:
public class Third extends AppCompatActivity {
EditText editText_Data;
Button button_SendData;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
editText_Data = findViewById(R.id.editText);
button_SendData = findViewById(R.id.button3);
button_SendData.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
}
public void finish() {
String name;
name = editText_Data.getText().toString();
Intent i = new Intent(Third.this, MainActivity.class);
i.putExtra("name", name);
setResult(RESULT_OK, i);
super.finish();
}
}
just remove finish(); thats it
The reason that it goes to the second activity is because of this:
button_SendData.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
public void finish() {
String name;
name = editText_Data.getText().toString();
Intent i = new Intent(Third.this, MainActivity.class);
i.putExtra("name", name);
setResult(RESULT_OK, i);
super.finish(); //This kills the current activity.
}
You should do:
public void finish() {
String name;
name = editText_Data.getText().toString();
Intent i = new Intent(Third.this, MainActivity.class);
Bundle bundle = new Bundle();
bundle.putString("name", name);
startActivityForResult(i, RESULT_OK, bundle);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent i) {
if((requestCode == 1) &&(resultCode == RESULT_OK)) {
Bundle bundle = i.getExtras();
String name = bundle.getString("name");
textViewInformation.setText(name);
}
}
When you call finish, it just kills the current activity. If you want to go back to the first activity, just start a new activity for the first activity and pass the data in a Bundle.
If you want to have more of a stack concept, you can use Fragments.

onActivityResult seems to be not working

MainActivity.java - Main class
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ImageView ClockButton;
private LinearLayout linearLayout;
private TextView[] textViews;
private int hours;
private int minutes;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViews=new TextView[20];
linearLayout=(LinearLayout)findViewById(R.id.LinearLayout1);
ClockButton=(ImageView)findViewById(R.id.ClockIconButton);
ClockButton.setOnClickListener(this);
}
public void onChangeContentView()
{
Intent intent = new Intent(this, MainActivity2.class);
this.startActivityForResult(intent,1);
}
#Override
public void onClick(View v) {
onChangeContentView();
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Toast.makeText(getApplicationContext(),"test",Toast.LENGTH_SHORT).show();//test
hours=Integer.parseInt(data.getStringExtra("H"));
minutes=Integer.parseInt(data.getStringExtra("M"));
for(int i=0;i<textViews.length;i++)
{
if(textViews[i]==null)
{
textViews[i].setText(hours+" : "+minutes);
linearLayout.addView(textViews[i]);
break;
}
}
}
}
MainActivity2.java - Sub class
public class MainActivity2 extends AppCompatActivity implements View.OnClickListener {
private ImageView OkButton;
private TimePicker timePicker;
private int hours;
private int minutes;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
OkButton = (ImageView) findViewById(R.id.OkIcon);
OkButton.setOnClickListener(this);
timePicker = (TimePicker) findViewById(R.id.TimePicker1);
}
#Override
public void onClick(View v) {
hours = timePicker.getCurrentHour();
minutes = timePicker.getCurrentMinute();
Intent intent = new Intent();
intent.putExtra("H", hours);
intent.putExtra("M", minutes);
setResult(1, intent);
finish();
}
}
I've tried to fix it. It's no problem, but I can't see the the Toast which is called in onActivityResult() in my Main class. (Toast.makeText(getApplicationContext(),"test",Toast.LENGTH_SHORT).show();)
What am I missing out on?
int RESULT_OK
Standard activity result: operation succeeded.
Constant Value: -1 (0xffffffff)
android developer site
Do like this:
setResult(RESULT_OK);

savedInstanceState causing a crash while screen rotation

public class CheatActivity extends AppCompatActivity {
private static final String EXTRA_ANSWER_IS_TRUE="com.example.ferhat.geoquiz.answer_is_true";
public static final String EXTRA_ANSWER_SHOWN="com.example.ferhat.geoquiz.answer_shown";
private static final String CHEATER="com.example.ferhat.geoquiz.cheated";
private Boolean mAnswerIsTrue;
private TextView mAnswerTextView;
private Button mShowAnswer;
private Boolean mIsCheater;
public static Intent newIntent(Context packageContext, boolean answerIsTrue){
Intent i=new Intent(packageContext,CheatActivity.class);
i.putExtra(EXTRA_ANSWER_IS_TRUE,answerIsTrue);
return i;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cheat);
mAnswerIsTrue = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false);
mAnswerTextView = (TextView) findViewById(R.id.answerTextView);
mShowAnswer = (Button) findViewById(R.id.showAnswerButton);
mShowAnswer.setOnClickListener(new View.OnClickListener() {
//Cevabı gösteriyor ve Kopya çekildi bilgisi veriliyor
#Override
public void onClick(View v) {
if (mAnswerIsTrue) {
mAnswerTextView.setText(R.string.true_button);
} else {
mAnswerTextView.setText(R.string.false_button);
}
mIsCheater=true;
setAnswerShownResult();
}
});
if(savedInstanceState!=null){
mIsCheater=savedInstanceState.getBoolean(CHEATER,false);
}
}
private void setAnswerShownResult(){
Intent data=new Intent();
data.putExtra(EXTRA_ANSWER_SHOWN,mIsCheater);
setResult(RESULT_OK,data);
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState){
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putBoolean(CHEATER,mIsCheater);
}
}
I try to solve challenge in Anroid Programming, Big Nerds Ranch Guide (Chap.5)
Challenge asks me to keep Cheat data while rotation of screen and transaction between questions.Main Activity holds questions and CheatActivity has answers from Main activity. And i created BooleanArray to hold cheat data for questions.
Problem is ,i cheated for first question and then when i am in the CheatActivity(CheatPage) of other questions ,program crashes if i rotate the screen.
Error caused by this line savedInstanceState.putBoolean(CHEATER,mIsCheater);
i think i need to clear data from previous Cheat Data(BooleanArray already holding it) but i dont know how to do it.
public class CheatActivity extends AppCompatActivity {
private static final String EXTRA_ANSWER_IS_TRUE = "com.example.ferhat.geoquiz.answer_is_true";
public static final String EXTRA_ANSWER_SHOWN = "com.example.ferhat.geoquiz.answer_shown";
public static final String EXTRA_CHEATED = "com.example.ferhat.geoquiz.cheated";
private static final String CHEATER = "com.example.ferhat.geoquiz.cheated";
private Boolean mAnswerIsTrue;
private TextView mAnswerTextView;
private Button mShowAnswer;
private Boolean mAnswerEverShown;
private Boolean twoStep=false;
//Yeni intent methodu yarattık Cevabı alıyor ve bu activity i başlatıyor
public static Intent newIntent(Context packageContext, boolean answerIsTrue, boolean checked) {
Intent i = new Intent(packageContext, CheatActivity.class);
i.putExtra(EXTRA_ANSWER_IS_TRUE, answerIsTrue);
i.putExtra(EXTRA_CHEATED, checked);
return i;
}
private void setAnswerShownResult(Boolean isAnswerShown) {
Intent data = new Intent();
**if(mAnswerEverShown) {
isAnswerShown=mAnswerEverShown;
data.putExtra(EXTRA_ANSWER_SHOWN, isAnswerShown);
setResult(RESULT_OK, data);
}else {
data.putExtra(EXTRA_ANSWER_SHOWN, isAnswerShown);
setResult(RESULT_OK, data);
}
twoStep=isAnswerShown;**
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cheat);
mAnswerIsTrue = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false);
mAnswerTextView = (TextView) findViewById(R.id.answerTextView);
**mAnswerEverShown = getIntent().getBooleanExtra(EXTRA_CHEATED, false);**
mShowAnswer = (Button) findViewById(R.id.showAnswerButton);
mShowAnswer.setOnClickListener(new View.OnClickListener() {
//Cevabı gösteriyor ve Kopya çekildi bilgisi veriliyor
#Override
public void onClick(View v) {
if (mAnswerIsTrue) {
mAnswerTextView.setText(R.string.true_button);
} else {
mAnswerTextView.setText(R.string.false_button);
}
twoStep=true;
setAnswerShownResult(twoStep);
}
});
**if (savedInstanceState != null) {
setAnswerShownResult(savedInstanceState.getBoolean(CHEATER, false));
}
}
#Override
public void onSaveInstanceState (Bundle savedInstanceState){
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putBoolean(CHEATER, twoStep);
}
}**
i found solution like for every situation.
first think i did; i get info from main activity
mAnswerEverShown = getIntent().getBooleanExtra(EXTRA_CHEATED, false);
And Then i changed setAnswerShownResult for two situation.If it is not cheated ever program sends current data(cheated or not).
i marked where i changed with *.
You need to putBoolean before callback the super method.
#Override
public void onSaveInstanceState(Bundle savedInstanceState){
savedInstanceState.putBoolean(CHEATER,mIsCheater);
super.onSaveInstanceState(savedInstanceState);
}
if not the CHEATER won't be saved and you can't call it when resume activity

Android - Adding item to ListView from another Activity

First of all, I've searched on many other posts and still not found a fix for it.
MainActivity contains a ListView and an ImageButton that takes to AddActivity.
This AddActivity has got a EditText (nameAddInput) and a Button(addButton).
Despite clicking this Button, the ListView in MainActivity remains empty... Don't understand why...
Here is the the code of MainActivity:
public class MainActivity extends AppCompatActivity {
static final int PICK_CONTACT_REQUEST = 0;
private ListView list;
private ArrayAdapter<String> adapter;
private ArrayList<String> arrayList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.itemsList);
arrayList = new ArrayList<>();
adapter = new ArrayAdapter<>(this, R.layout.listview_style1, android.R.id.text1, arrayList);
}
public void onClickAddButton(View view) {
Intent i = new Intent(MainActivity.this, AddActivity.class);
startActivityForResult(i, 2);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_CONTACT_REQUEST) {
if (resultCode == RESULT_OK) {
addNewItem();
}
}
}
public void addNewItem() {
Bundle addNameInfo = getIntent().getExtras();
if(addNameInfo == null)
return;
String nameInput = addNameInfo.getString("nameInput");
arrayList.add(nameInput);
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
In xml file of MainActivity in the ImageButton: android:onClick="onClickAddButton"
The code of AddActivity:
public class AddActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
}
public void backToMain(View view) {
Intent i = new Intent();
EditText nameAddInput = (EditText) findViewById(R.id.nameAddInput);
String userNameText = nameAddInput.getText().toString();
i.putExtra("nameInput", userNameText);
setResult(RESULT_OK, i);
finish();
}
}
In xml file of AddActivity in the Button: android:onClick="backToMain"
Hope someone can help!!
Thank you in advance!!
getIntent() returns you the intent that launched MainActivity, not the one you set in backInMain
Try the "data" variable passed to you in onActivityResult?
Also change to
startActivityForResult(i, PICK_CONTACT_REQUEST);
I'd also suggest you rename that variable 😉
set adapter just in onCreate and just call notifyDataSetChanged in addNewItem method.
the resulting value is returned in the Intent data-Parameter of the onActivityResult function, not in the intent-member of the mainActivity.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_CONTACT_REQUEST) {
if (resultCode == RESULT_OK) {
addNewItem(data.getExtras().getString("nameInput");
}
}
}
...
public void addNewItem(String newItem)
...

Using Conditions to Pass Data between Activities

Successfully I tried to pass data between three Activities.
That is :
(Data3)Activity1(Data1)-->(Data1)Activity2(Data2)-->(Data2)Activity3)
Now the Problem is:
I want pass to data between these activities using conditions. That
is in Activity2, before sending data to Activity3 I want to check
WORD = "word building"
DROP = "word built"
if WORD FROM EDITTEXT == WORD
pass data to Activity1 AND
goto Activity1
else
if WORD FROM EDITTEXT == DROP
pass data to Activity3 AND
goto Activity3
Here is the code for Activity1 named PickCard.java
public class PickCard extends Activity {
String card = "Card Picked";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
displayIntentData();
findViewById(R.id.sendButton).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(PickCard.this, BuildWord.class);
intent.putExtra("key", card);
startActivity(intent);
}
});
}
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);//must store the new intent unless getIntent() will return the old one
displayIntentData();
}
private void displayIntentData() {
Intent intent = getIntent();
TextView tv = (TextView) findViewById(R.id.intentData);
Bundle extras = intent.getExtras();
if (extras != null) {
tv.setText("Data received: " + extras.getString("key"));
} else {
tv.setText("No extradata received");
}
}
}
Here is the code for Activity2 BuildWord.java
public class BuildWord extends Activity {
String word = "Word Building";
String finished = "Word Built";
EditText simulate = (EditText) findViewById(R.id.dataToSend);
String getdata = simulate.getText().toString();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.buildword);
displayIntentData();
if (getdata == word) {
findViewById(R.id.sendButton1).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(BuildWord.this, MainActivity.class);
intent.putExtra("key", getdata);
startActivity(intent);
}
});
} else {
findViewById(R.id.sendButton1).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(BuildWord.this, DropCard.class);
intent.putExtra("key", finished);
startActivity(intent);
}
});
}
}
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
displayIntentData();
}
private void displayIntentData() {
Intent intent = getIntent();
TextView tv = (TextView) findViewById(R.id.intentData1);
Bundle extras = intent.getExtras();
if (extras != null) {
tv.setText("Data received: " + extras.getString("key"));
} else {
tv.setText("No extradata received");
}
}
}
Here is the code for Activity3 named DropCard.java
public class DropCard extends Activity {
String drop = "Card Dropped";
String declared = "User Declared";
boolean won = false;
/*Intent intent = getIntent();
TextView tv = (TextView)findViewById(R.id.intentData2);
Bundle extras=intent.getExtras();*/
String get = "word building";//extras.getString("key");
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dropcard);
displayIntentData();
if (get == "word built") {
findViewById(R.id.sendButton2).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(DropCard.this, Declare.class);
intent.putExtra("key", declared);
startActivity(intent);
}
});
} else {
findViewById(R.id.sendButton2).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(DropCard.this, MainActivity.class);
intent.putExtra("key", drop);
startActivity(intent);
//notice we dont call finish() here
}
});
}
}
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);//must store the new intent unless getIntent() will return the old one
displayIntentData();
}
private void displayIntentData() {
Intent intent = getIntent();
TextView tv = (TextView) findViewById(R.id.intentData2);
Bundle extras = intent.getExtras();
if (extras != null) {
tv.setText("Data received: " + extras.getString("key"));
} else {
tv.setText("No extradata received");
}
}
}
use
if (getdata.equals(word)){
}
instead of
if (getdata == word){
}
for comparing Strings.as in your current code replace all == with equals
if (getData.getText().toString().equalsIgnoreCase("String name"))//change this line in your code for checking
{
findViewById(R.id.sendButton1).setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent(BuildWord.this,MainActivity.class);
intent.putExtra("key", getdata);
startActivity(intent);
}
});
}
else
{
findViewById(R.id.sendButton1).setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent(BuildWord.this,DropCard.class);
intent.putExtra("key", finished);
startActivity(intent);
}
});
}
Its a simple you have to compare in that default method equalsIgnoreCase().
String name = edit_text.getText().toString();
if(name.equalsIgnoreCase("WellCome"))
{
//CALL YOUR ACTIVITY ONE
}
else
{
//CALL YOUR ACTIVITY THREE
}

Categories