CharSequence passed through intent arrives modified - android

I'm sending a CharSequence of a Spannable to another Activity via intent extras, but seem not to receive the same sequence.
I'm doing that according to the following answer: https://stackoverflow.com/a/45638248/1545435
Here's the content of learnMoreText and how I'm sending it. It has 12 spans:
#Override
public void onItemLearnMoreClick(View view, int position) {
Intent intent = new Intent(getContext(), LearnMoreActivity.class);
CharSequence learnMoreText = model.getLearnMoreText(position);
intent.putExtra(LearnMoreActivity.EXTRA_LEARN_MORE, learnMoreText);
String learnMoreType = model.getLearnMoreType(position);
intent.putExtra(LearnMoreActivity.EXTRA_LEARN_MORE_TYPE, learnMoreType);
startActivity(intent);
}
Here's how I receive it in the LearnMoreActivity. Now it only contains 10 spans. The link-spans <a></a> were striped:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivityComponent().inject(this);
LearnMoreActivityBinding binding = DataBindingUtil.setContentView(this, R.layout.learn_more_activity);
CharSequence learnMoreHtmlText = getIntent().getCharSequenceExtra(EXTRA_LEARN_MORE);
binding.contentText.setText(learnMoreHtmlText);
binding.contentText.setLinksClickable(true);
binding.contentText.setMovementMethod(LinkMovementMethod.getInstance());
setupToolbar();
}
Can anyone point to the reason and how to fix?

Related

Android studio how to pass Array data to another activity's TextView?

I am new to learning Array in android studio. Please show me some examples in details. I have write an example here and I want to display the Array data from MainActivity into second_page activity .
MainActivity.java
public class MainActivity extends AppCompatActivity {
String my_array[]={"dog","cat","tiger"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void next_page(View view){
Intent intent = new Intent(this,second_page.class);
intent.putExtra("my_array_next", my_array);
startActivity(intent);
}
}
second_page.java
public class second_page extends MainActivity {
TextView get_data;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_page);
get_data=(TextView)findViewById(R.id.tv);
Intent intent=getIntent();
// coding here to display the array data
// sth like abc.setText(display_array_data);
}
Please advice. Thank you!
If you are trying to send a String-array from one Activity to another this can be done in the Intent.
In ClassA:
Intent intent = new Intent(this, ClassB);
String[] my_array = new String[]{"dog","cat","tiger"};
intent.putExtra("myArr", my_array);
startActivity(intent);
In ClassB:
public void onCreate() {
Intent intent = getIntent();
String[] myStrings = intent.getStringArrayExtra("myArr");
}
this may helps you
In second_page.java, receive the array you pass via Intent and set it to your TextView like this
#Override
protected void onCreate(Bundle savedInstanceState) {
...
String[] array = intent.getStringArrayExtra("my_array_next");
// TextView display a String so you should convert your Array to String
String str1 = Arrays.toString(array);
get_data.setText(str1);
}
First take the array:
Intent intent = getIntent();
List array;
if (intent.getExtras() != null) {
array= intent.getExtras().getBoolean("my_array_next");
}
Then print
get_data.setText(array.toString());
Sending Class:
Intent intent = new Intent(this, ClassB);
String[] myStrings = new String[] {"test", "test2"};
intent.putExtra("strings", myStrings);
startActivity(intent);
Reciving Class:
public void onCreate() {
Intent intent = getIntent();
String[] myStrings = intent.getStringArrayExtra("strings");
}
In your Second activity:
String[] array=getIntent().getStringArrayExtra("my_array_next");
I think you need to go through basics, go to https://developer.android.com/index.html to get started.

Android Activity create and start

I'm currently writting an app for Android. I'm trying to figure out the best place to catch on intent when the activity is created for the first time.
public class DisplayAct extends Activity {
private Intent mNewIntent;
private ViewFinderFragment mViewFinderFrag;
public boolean toot;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED, WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
setContentView(R.layout.activity_display);
if(savedInstanceState != null) {
Intent intent = getIntent();
if (intent != null) {
String action = intent.getAction();
}
}
This is how I get the intent.
I was wondering if grabbing the intent at oncreate is good or it's better to add onStart and add this action inside in term of design perspective.
Thanks
If you app is open, handle it with onNewIntent. If you app is closed, you wanna use your Bundle that comes with your Intent.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState.getString(MY_ACTION)) {
// Do something
}
EDIT:
I set this action as a String to the Intent's Bundle. I performed it with the code below:
Bundle bundle = new Bundle();
bundle.putString(MainActivity.MY_ACTION, "MY_ACTION");
Intent intent = new Intent(this, MainActivity.class);
intent.putExtras(bundle);

Passing Object with multiple parts from one activity to another

Hi I have a Requirement like Person Details for that i have multiple details like personal,educational,job like In first activity am enter personal details and then educational and then job like that one after another for that how can i maintain all details in single object and pass through app please help me.
Be aware that you need to bundle an object in an Intent, that object must implement the Parcelable interface. Here's a common implementation taken from the Parcelable documentation...
public class MyParcelable implements Parcelable {
private int mData;
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel out, int flags) {
out.writeInt(mData);
}
public static final Parcelable.Creator<MyParcelable> CREATOR
= new Parcelable.Creator<MyParcelable>() {
public MyParcelable createFromParcel(Parcel in) {
return new MyParcelable(in);
}
public MyParcelable[] newArray(int size) {
return new MyParcelable[size];
}
};
private MyParcelable(Parcel in) {
mData = in.readInt();
}
}
Once you've done that, simply pass the object around in an Intent...
Intent i = new Intent(getApplicationContext(), ACTIVITY_TO_START.class);
i.putExtra("extra_key", new MyParcelable());
startActivity(i);
to retrieve the object for the starting activity...
Bundle extras = getIntent().getExtras();
if(extras != null && extras.containsKey("extra_key"))
{
MyParcelable p = (MyParecelable)extras.getParcelable("extra_key");
}
Simple solution: Use intents for this
Personal.class
public class Personal extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.personal);
Intent i = new Intent(getApplicationContext(), Educational.class);
i.putExtra("personal_details",<-get data from object->);
startActivity(i);
}
}
Educational.class
public class Personal extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.personal);
String personal_details;
Bundle extras = getIntent().getExtras();
if (extras != null) {
personal_details= extras.getString("personal_details");
}
Intent i = new Intent(getApplicationContext(), educational.class);
i.putExtra("personal_details",personal_details);
i.putExtra("educational_details",<-get data from object->);
startActivity(i);
}
}
Job.class
public class Personal extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.personal);
String personal_details,educational_details;
Bundle extras = getIntent().getExtras();
if (extras != null) {
personal_details= extras.getString("personal_details");
educational_details= extras.getString("educational_details");
}
Intent i = new Intent(getApplicationContext(), FinalResult.class);
i.putExtra("personal_details",personal_details);
i.putExtra("educational_details",educational_details);
i.putExtra("job_details",<-get data from object->);
startActivity(i);
}
}
FinalResult.class
public class Personal extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.personal);
String personal_details,educational_details,job_details;
Bundle extras = getIntent().getExtras();
if (extras != null) {
personal_details= extras.getString("personal_details");
educational_details= extras.getString("educational_details");
job_details= extras.getString("job_details");
}
}
}
{EDIT-1}
have a look at one of my answers in different storage options in
android - Click Here
You can achieve it using application variable or shared
preferences but i would not recommend it. !
Try storing it in POJO(Plain old Java class)
{EDIT-2}
Hmm if i understand correctly your question:: You are connected to internet(Wifi,Wired,... etc) basically you are trying to show a dialog when there is no network connectivity ! .... You can take the help of Broadcast receivers ...
Try this:: Set the broadcast receiver to fire the intent when there is no net connectivity ....
Write the code to catch that intent and pop the dialog .... In this dialog give the user option to reconnect the connectivity !

is it possible to accept value from intent before oncreate

I had fragmented my code so as to follow oop concept thus a code that appears in four activities was made into a separate activity of its own .. now this child activities actions depends on from which of the four parent activities it was called.. so the setContent view value varies according to the parent activity..
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pdf_view);
Intent intent = getIntent();
String url = intent.getStringExtra("URL");
I got it...
all you have to do is to call the intent in the on create function before the setcontent view method..
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String url = intent.getStringExtra("URL");
String layout = intent.getStringExtra("layout");
int id =getResourceUsingName(layout,"layout");
setContentView(id);
}
private int getResourceUsingName(String name, String type) {
mContext=getApplicationContext();
Resources resources = mContext.getResources();
int resourceId = resources.getIdentifier(name, type,
mContext.getPackageName());
return resourceId;
}

onActivityResult method not working.

I have a small application that launches a "login" activity as a sub activity from my main application activity. The login activity collects username and password using EditText fields and then sends the credentials to main activity for validation after clicking a submit button. For some reason though, my onActivityResult() is not working. I even tried including a TextView that I would manipulate at different points of the method to deduce where it is going wrong, but it appears that the method itself isn't executing for some reason. Any help would be much appreciated.
My MainActivity:
public class MainActivity extends Activity {
private final String userName = "9590778";
private final String userPass = "1234567";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void activity_about(View view){
Intent aboutIntent = new Intent(this, AboutActivity.class);
startActivity(aboutIntent);
}
public void activity_login(View view){
Intent loginIntent = new Intent(this, LoginActivity.class);
startActivityForResult(loginIntent, LoginActivity.LOGIN_REQUEST);
}
public void OnActivityResult(int requestCode, int responseCode, Intent resultIntent){
String passedUser;
String passedPass;
Intent intent = getIntent();
TextView lblTest = (TextView)findViewById(R.id.lblTest);
lblTest.setText("ATLEAST THIS IS WORKING!"); //THIS IS WHERE I AM TRYING TO MANIPULATE THE TEXTVIEW TO SEE IF METHOD IS EXECUTING!
if(requestCode == LoginActivity.LOGIN_REQUEST){
passedUser = intent.getStringExtra("PASSED_USER");
passedPass = intent.getStringExtra("PASSED_PASS");
if(passedUser.equals(userName)){
if(passedPass.equals(userPass)){
lblTest.setText("LOGIN SUCCESSFUL!");
}
}
else{
lblTest.setText("ACCESS DENIED!");
}
}
}
}
Also, this is my LoginActivity:
public class LoginActivity extends Activity {
public static final int LOGIN_REQUEST = 9999;
public static final int LOGIN_RESPONSE = 1234;
private String passedUser;
private String passedPass;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_login, menu);
return true;
}
public void submitCred(View view){
Intent intent = getIntent();
EditText lblUser = (EditText)findViewById(R.id.txtUserName);
EditText lblPass = (EditText)findViewById(R.id.txtPassword);
passedUser = lblUser.getText().toString();
passedPass = lblPass.getText().toString();
intent.putExtra("PASSED_USER", passedUser);
intent.putExtra("PASSED_PASS", passedPass);
setResult(LOGIN_RESPONSE, intent);
finish();
}
}
You have a typo, the function you want is onActivityResult, (notice the lowercase o).
It's a good habit to use the #Override statement before any method you want to override. If you put #Override before OnActivityResult in your case it would complain at you that OnActivityResult is not a superclass method.
It's a good check to make sure you have the right name and right parameters.

Categories

Resources