I have Activity1, Activity1 Adapter and Activity2
I'm not able to pass value between an Adapter and Activity. When back button is pressed, I'm expecting a value to be coming from the Second Activity (Activity 2) to Activity1 Currently, it gives me null
Here are my code snippets.
Activity1 Adapter
holder.cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Activity origin = (Activity) context;
Intent intent = new Intent(context, PostActivity.class);
intent.putExtra("searchText", staggeredCustomCard.getSearchText());
origin.startActivityForResult(intent, 1);
}
});
Activity2
#Override
public void onBackPressed() {
super.onBackPressed();
Intent mIntent = new Intent();
mIntent.getStringExtra("searchText");
setResult(1, mIntent);
}
I'm expecting this searchText to be going to Activity1. Could anyone please guide me how to achieve this?
You are getting getStringExtra("searchText"); from a completely new intent, that's why it's returning null. You need to get search text from getIntent() like this:
#Override
public void onBackPressed() {
super.onBackPressed();
Intent mIntent = new Intent();
String search = getIntent().getStringExtra("searchText");
mIntent.putExtra("searchText", search);
setResult(1, mIntent);
}
I'm doing the Android Fundamentals 2.2 Coding Challenge and I'm unable to get the second activity to launch using the logic described in the preceding lessons.
Here is the code for my first activity:
package com.homing.a22codingchallenge;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private static final String LOG_TAG = MainActivity.class.getSimpleName();
public static final String EXTRA_MESSAGE = "com.homing.mainactivity.extra.message";
public static final int TEXT_REQUEST = 1;
private TextView TV1, TV2, TV3, TV4, TV5, TV6, TV7, TV8, TV9, TV10;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TV1 = findViewById(R.id.TV1);
TV2 = findViewById(R.id.TV2);
TV3 = findViewById(R.id.TV3);
TV4 = findViewById(R.id.TV4);
TV5 = findViewById(R.id.TV5);
TV6 = findViewById(R.id.TV6);
TV7 = findViewById(R.id.TV7);
TV8 = findViewById(R.id.TV8);
TV9 = findViewById(R.id.TV9);
TV10 = findViewById(R.id.TV10);
}
public void addItems(View view) {
Log.d(LOG_TAG, "Button clicked");
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra(EXTRA_MESSAGE, "What?");
startActivityForResult(intent, TEXT_REQUEST);
Log.d(LOG_TAG, "startActivityForResult()");
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TEXT_REQUEST) {
if (resultCode == RESULT_OK) {
String reply = data.getStringExtra(SecondActivity.EXTRA_RETURN);
fillList(reply);
}
}
}
public void fillList(String string) {
String[] list = { TV1.toString(), TV2.toString(), TV3.toString(), TV4.toString(), TV5.toString(), TV6.toString(), TV7.toString(), TV8.toString(), TV9.toString(), TV10.toString() };
for (int i = 0; i < list.length; i++) {
list[i] = string;
}
}
}
Here is the code to my second activity:
package com.homing.a22codingchallenge;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
public class SecondActivity extends AppCompatActivity {
private Button BTN1, BTN2, BTN3, BTN4, BTN5, BTN6, BTN7, BTN8, BTN9, BTN10;
public static final String EXTRA_RETURN = "com.homing.22codingchallenge.secondactivity.extra.return";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
BTN1 = findViewById(R.id.BTN1);
BTN2 = findViewById(R.id.BTN2);
BTN3 = findViewById(R.id.BTN3);
BTN4 = findViewById(R.id.BTN4);
BTN5 = findViewById(R.id.BTN5);
BTN6 = findViewById(R.id.BTN6);
BTN7 = findViewById(R.id.BTN7);
BTN8 = findViewById(R.id.BTN8);
BTN9 = findViewById(R.id.BTN9);
BTN10 = findViewById(R.id.BTN10);
Intent returnIntent = new Intent();
returnIntent.putExtra(EXTRA_RETURN, BTN1.getText().toString());
setResult(RESULT_OK, returnIntent);
finish();
}
}
I've tried comparing the code with the project I was walked through in the guide and everything is consistent so far as I can see. Posts with issues similar to mine made a few suggestions that didn't make sense since my first project worked fine.
I've let up debug logs and confirmed in Logcat that the button is registering the click and it's even running through the block through the startActivityForResult() method.
There was one Logcat entry that seemed relevant, but searched didn't really yield anything helpful to me:
2018-10-18 07:01:37.386 1624-1677/system_process W/ActivityManager: Unable to start service Intent { act=com.google.android.gms.drive.ApiService.RESET_AFTER_BOOT flg=0x4 cmp=com.google.android.gms/.drive.api.ApiService (has extras) } U=0: not found
I've since tried to reproduce this error a number of times, but have not been able to. The only entries I'm seeing across my attempts are along the lines of the following:
2018-10-18 07:00:44.979 1369-1401/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 754681 , only wrote 603360
But I'm not sure that this is really related to the issue of launching the second activity.
Edit:
In response to some comments here is my manifest.
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="22CodingChallenge"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:label="#string/SecondActivity_name"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.homing.a22codingchallenge.MainActivity" />
</activity>
</application>
If your onActivityResult is reached that means the SecondActivity has been started. You have just to check what to do inside your second activity before finishing it. For example with the code above, you are calling finishing the activity at its creation.
Your code works, you have just to find the right place to place this code:
Intent returnIntent = new Intent();
returnIntent.putExtra(EXTRA_RETURN, BTN1.getText().toString());
setResult(RESULT_OK, returnIntent);
finish();
According to your logic.
I think this will help.
If the second Activity is not added to the AndroidManifest.xml, IDE will complaining that it is not added.
However, if you get: Button clicked in the logs, so main Activity has no problem but, check the codes in the second Activity:
Intent returnIntent = new Intent();
returnIntent.putExtra(EXTRA_RETURN, BTN1.getText().toString());
setResult(RESULT_OK, returnIntent);
finish();
As soon as it do the putExtra(), it actually finishes the Activity: finish(); after.
For what I see, I suppose you are just starting the SecondActivity in an onClick attribute in the activity_main layout, and, once in the SecondActivity, as soon as the onCreate happens, you are just calling finish() here:
Intent returnIntent = new Intent();
returnIntent.putExtra(EXTRA_RETURN, BTN1.getText().toString());
setResult(RESULT_OK, returnIntent);
finish();
So as soon as the activity is created, it is finished...
Maybe what you are trying to do is to return the button clicked text in the SecondActivity?
Something like:
BTN1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent returnIntent = new Intent();
returnIntent.putExtra(EXTRA_RETURN, BTN1.getText().toString());
setResult(RESULT_OK, returnIntent);
finish();
}
});
Maybe you should add a onClick attribute to each button, and add a call to a method that will obtain the text of the button clicked like:
public void clickButton(View view) {
Intent returnIntent = new Intent();
returnIntent.putExtra(EXTRA_RETURN, ((Button) view).getText().toString());
setResult(RESULT_OK, returnIntent);
finish();
I haven't tried the code, but I hope this helps you!
Hei guys. I am new to Android programming. I am trying to make an app that controls a rc car (Arduino). In this app are the next activities:
Button Commands
Tilt Command
Vocal Command
Instructions
About
When I open the app it suddenly stops and I don't know why. Can someone help me by looking at the code and tell me what I am doing wrong. Thanks a lot.
Button buttonCommand;
Button tiltCommand;
Button vocalCommand;
Button instructions;
Button about;
Button arduino; // Links Button
Button android; // Links Button
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
buttonCommand = (Button)findViewById(R.id.buttons);
tiltCommand = (Button)findViewById(R.id.tilt);
vocalCommand = (Button)findViewById(R.id.vocal);
instructions = (Button)findViewById(R.id.instructions);
about = (Button)findViewById(R.id.about);
arduino = (Button)findViewById(R.id.arduino);
android = (Button)findViewById(R.id.android);
}
public void onClickButton(View view){
Intent i = new Intent(getApplicationContext(),ButtonCommand.class); // ButtonCommand Activity
startActivity(i);
}
public void onClickTilt(View view){
Intent i = new Intent(getApplicationContext(),TiltCommand.class); // TiltCommand Activity
startActivity(i);
}
public void onClickVocal(View view){
Intent i = new Intent(getApplicationContext(),VocalCommand.class); // VocalCommand Activity
startActivity(i);
}
public void onClickInstructions(View view){
Intent i = new Intent(getApplicationContext(),Instructions.class); // Instructions Activity
startActivity(i);
}
public void onClickAbout(View view){
Intent i = new Intent(getApplicationContext(),About.class); // About Activity
startActivity(i);
}
public void onClickArduino(View view){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse("http://forum.arduino.cc/"));
startActivity(intent);
}
public void onClickAndroid(View view){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse("http://forum.xda-developers.com/"));
startActivity(intent);
}
If your app stops at the start, probably you are missing to add the registry of your activities into your AndroidManifest.xml :
<activity android:name=".ButtonCommand" />
<activity android:name=".TiltCommand" />
<activity android:name=".VocalCommand" />
<activity android:name=".Instructions" />
<activity android:name=".About" />
George, you need to create an OnClickListener for each button for example:
buttonCommand = (Button)findViewById(R.id.buttons);
buttonCommand.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),ButtonCommand.class); // ButtonCommand Activity
startActivity(i);
}
});
Plus: and the internet permission to open urls:
<uses-permission android:name="android.permission.INTERNET" />
public void onClick(View view) {
switch(view.getId()) {
case R.id.button1:
Intent intent = new Intent(this, java1.class);
startActivity(intent);
break;
case R.id.button2:
Intent i = new Intent(this, java2.class);
startActivity(i);
break;
}
}
the buttons don't seem to work at all and the app crashes when button is pressed, i have 2 buttons that when clicked go to different activities fro the main activity.
Try declaring intent at Class Level and refer it in onCreate() ,then just use startActivity in onClick()
Intent intent,i; //Globally
onCreate(Bundle savedInstances)
{
...
i=new Intent(getApplicationContext(),java2.class);
intent=new Intent(getApplicationContext(),java1.class);
...
}
Then in onClick
public void onClick(View view) {
switch(view.getId()) {
case R.id.button1:
yourclassname.this.startActivity(intent);
break;
case R.id.button2:
yourclassname.this.startActivity(i);
break;
}
}
And make sure your java1 and java2 classes are of type Activity/ListActivity
Im new on Java so its confusing me a little bit, i want to create an intent for Activity two but there seem to be a problem with the code that i have written.
#Override
public void onClick(View v) {
// TODO:
// Launch Activity Two
// Hint: use Context's startActivity() method
// Create an intent stating which Activity you would like to
// start
Intent activityTwo = new Intent(ActivityTwo.this.finish());
Intent intent = null;
// Launch the Activity using the intent
startActivity(activityTwo);
}
});
// Has previous state been saved?
if (savedInstanceState != null) {
// TODO:
// Restore value of counters from saved state
super.onRestoreInstanceState(savedInstanceState);
mCreate = savedInstanceState.getInt(CREATE_KEY);
mRestart = savedInstanceState.getInt(RESTART_KEY);
mStart = savedInstanceState.getInt(START_KEY);
}
// Emit LogCat message
Log.i(TAG, "Entered the onCreate() method");
// TODO:
from First to Second:
Button next = (Button) findViewById(R.id.button2);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(),Second.class);
intent.putExtra("Tag", "Value");
startActivity(intent);
finish();
}});
Second to First:
Button previous= (Button) findViewById(R.id.button);
previous.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(),First.class);
startActivity(intent);
}});
First Understand the concept of Intent.
In your case, you want to call ActivityTwo from ActivityOne.
follow this steps
Create ActivityTwo.
Declare ActivityTwo in your AndroidManifest.xml
<activity android:name=".ActivityTwo" />
Write Intent code where in onclick() method.
Intent intent = new Intent(getApplicationContext(),ActivityTwo.class);
startActivity(intent);
In this code Intent Constructor contains two parameters.
Current Context
ActivityTwo reference.