I´m trying to send a parcelabe object from second activity to main activity, but the main activity is still in the Activities´ Stack, so I have two main activity running, I know this because I have a button to close the app and I have to press the button twice, one per each main activity.
Is it possible to send a parcelable object to main activity without open again the main activity? Is it impossible or I must use sql Lite instead of?
Is other command to use instead of startActivity() to call an activity that is now in the activities stack?
My goal saves in a list the new document that the user has saved in the second activity and shown it in the main activity all the documents save.
I can recover the data send for the second activity to the main activity, but I have two main activity running...
Any advice?
Thanks in advance.
My code:
public class Albaran implements Parcelable {
private String numalbaran;
private boolean tienefoto;
private boolean incidencia;
private Uri imageToUploadUri;
public Albaran(){
}
public Albaran(String numalbaran, boolean tienefoto, boolean incidencia, Uri imageToUploadUri) {
this.numalbaran = numalbaran;
this.tienefoto = tienefoto;
this.incidencia = incidencia;
this.imageToUploadUri = imageToUploadUri;
}
protected Albaran(Parcel in) {
numalbaran = in.readString();
tienefoto = in.readByte() != 0;
incidencia = in.readByte() != 0;
imageToUploadUri = in.readParcelable(Uri.class.getClassLoader());
}
public static final Creator<Albaran> CREATOR = new Creator<Albaran>() {
#Override
public Albaran createFromParcel(Parcel in) {
return new Albaran(in);
}
#Override
public Albaran[] newArray(int size) {
return new Albaran[size];
}
};
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(numalbaran);
dest.writeByte((byte) (tienefoto ? 1 : 0));
dest.writeByte((byte) (incidencia ? 1 : 0));
dest.writeParcelable(imageToUploadUri, flags);
}
}
I call to the second activity in my main activity
public void recogerDatos(View view){ //Onclick
intent = new Intent(this,RecogerActivity.class);
startActivity(intent);
}
I call to the first activity from my second activity
public void guardarAlbaran(View view){ //Save data when button is pressed
Albaran NuevoAlbaran = new Albaran(numAlbaran.toString(),incidenciaCheckbox.isChecked(), incidenciaCheckbox.isChecked(), imageToUploadUri);
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("Albaran", NuevoAlbaran);
startActivity(intent);
finish(); //Finalizar Actividad
}
I read the data from the parcelable objects in my main activity
Albaran nuevoAlbaran;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nuevoAlbaran = getIntent().getParcelableExtra("Albaran");
}
Related
I am working on an application where i need to navigate to specific activity based on some actions. Here is a image
Here my first activity is the ReadingActivity. Based on some actions user will be taken to the NewProjectReadingActivity. There the user will have Two options.
Option One
Option Two
Based on the options chosen by the user he/she will be taken to the ReadingInputActivity. After taking the input from the ReadingInputActivity the user will be forwarded to the ReadingConfirmationActivity. There will be three options
Option One
Option Two
Option Three
If the user selects the Option One then he/she will be taken to the ReadingActivity which is very easy. I will clear the stack and start the ReadingActivity again and there if he/she presses the back button the app will be minimized which is totally fine. But if the user selects Option Two then he/she will be taken to the NewProjectReadingActivity.If i clear the activity stack and start NewProjectReadingActivity again then it will start the NewProjectReadingActivity but the problem is that if the user presses the back button it will minimize my app as it is the only Activity present in the activity stack.
What i want is that if the user selects Option Two on the ReadingConfirmationActivity then the user will be taken to the NewReadingActivity that means i want to remove ReadingConfirmationActivity (which is easy just call the finish()) and the activity started before that activity i,e ReadingInputActivity.
Every Activity mentioned above is hosting a fragment. I am providing the activity code below.
ReadingActivity
public class ReadingActivity extends BaseAppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if( savedInstanceState == null ) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, ReadingFragment.getInstance(), ReadingFragment.TAG).commit();
}
setDisplayHomeAsUpEnabled(true);
setActionBarText(getString(R.string.new_project_reading));
}
#Override
protected void setupContentView() {
setContentView(R.layout.activity_reading);
}
public static Intent newIntent(Context context) {
Intent intent = new Intent(context, ReadingActivity.class);
return intent;
}
}
NewProjectReadingActivity
public class NewProjectReadingActivity extends BaseAppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if( savedInstanceState == null ) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, NewProjectReadingFragment.getInstance(), NewProjectReadingFragment.TAG).commit();
}
setDisplayHomeAsUpEnabled(true);
setActionBarText(getString(R.string.new_project_reading));
}
#Override
protected void setupContentView() {
setContentView(R.layout.activity_new_project_reading);
}
public static Intent newIntent(Context context) {
Intent intent = new Intent(context, NewProjectReadingActivity.class);
return intent;
}
}
ReadingInputActivity
public class ReadingInputActivity extends BaseAppCompatActivity {
private static final String EXTRA_VALUE_TYPE = "value_type";
private int valueType = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
valueType = getIntent().getIntExtra(EXTRA_VALUE_TYPE, 0);
if( savedInstanceState == null ) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, ReadingInputFragment.getInstance(valueType), ReadingInputFragment.TAG).commit();
}
setDisplayHomeAsUpEnabled(true);
setActionBarText(getString(R.string.reading_input));
}
#Override
protected void setupContentView() {
setContentView(R.layout.activity_reading_input);
}
public static Intent newIntent(Context context, int valueType) {
Intent intent = new Intent(context, ReadingInputActivity.class);
intent.putExtra(EXTRA_VALUE_TYPE, valueType);
return intent;
}
}
ReadingConfirmationActivity
public class ReadingConfirmationActivity extends BaseAppCompatActivity {
private static final String EXTRA_VALUE_TYPE = "value_type";
private static final String EXTRA_READING_VALUE = "reading_value";
private int valueType = 0;
private double readingValue = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
valueType = getIntent().getIntExtra(EXTRA_VALUE_TYPE, 0);
readingValue = getIntent().getDoubleExtra(EXTRA_READING_VALUE, 0);
if( savedInstanceState == null ) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, ReadingConfirmationFragment.getInstance(valueType, readingValue), ReadingConfirmationFragment.TAG).commit();
}
setDisplayHomeAsUpEnabled(true);
setActionBarText(getString(R.string.reading_input));
}
#Override
protected void setupContentView() {
setContentView(R.layout.activity_reading_confirmation);
}
public static Intent newIntent(Context context, int valueType, double readingValue) {
Intent intent = new Intent(context, ReadingConfirmationActivity.class);
intent.putExtra(EXTRA_VALUE_TYPE, valueType);
intent.putExtra(EXTRA_READING_VALUE, readingValue);
return intent;
}
}
You wrote:
What i want is that if the user selects Option Two on the
ReadingConfirmationActivity then the user will be taken to the
NewReadingActivity that means i want to remove
ReadingConfirmationActivity (which is easy just call the finish()) and
the activity started before that activity i,e ReadingInputActivity.
To do this you will do the following when the user selects Option Two in ReadingConfirmationActivity:
Intent intent = new Intent(this, NewReadingActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
This will cause all of the activities on top (and including) NewReadingActivity to be finished. A new instance of NewReadingActivity will be created and shown to the user. If you wish to return to the existing instance of NewReadingActivity, you can also do that by also adding Intent.FLAG_ACTIVITY_SINGLE_TOP to the Intent.
I have three Activities. MainActivity,ActivityB and ActivityC. In activity A and B there are two buttons source and destination in both activities. in Activity C there is a list of data. when button is clicked (either Source or destination) from activity A and B. both Activities are calling Activity C
code for Activity A is following
public class MainActivity extends Activity {
TextView source,destination;
Button sendSource,sendDestination,btnTob;
String src,des,activity,checksrc,checkdes;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
source=(TextView)findViewById(R.id.tv_rcvDataA);
destination=(TextView)findViewById(R.id.tv_rcvDataAa);
sendSource=(Button)findViewById(R.id.btn_sendA);
sendDestination=(Button)findViewById(R.id.btn_sendAa);
btnTob=(Button)findViewById(R.id.btn_toB);
sendSource.setText("source");
sendDestination.setText("destination");
src=sendSource.getText().toString();
des=sendDestination.getText().toString();
activity=getClass().getSimpleName();
sendSource.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent send= new Intent(MainActivity.this,ActivityC.class);
send.putExtra("source",src);
send.putExtra("Activity",activity);
startActivity(send);
}
});
sendDestination.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent senda= new Intent(MainActivity.this,ActivityC.class);
senda.putExtra("destination",des);
senda.putExtra("Activity",activity);
startActivity(senda);
}
});
btnTob.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent movetoB= new Intent(MainActivity.this,ActivityB.class);
startActivity(movetoB);
finish();
}
}); }}
and code for Activity B is
public class ActivityB extends Activity {
TextView sourceB,destinationB;
Button sendSourceB,sendDestinationB;
String src,des,activity,checksrc,checkdes;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
sourceB=(TextView)findViewById(R.id.tv_rcvDataB);
destinationB=(TextView)findViewById(R.id.tv_rcvDataBa);
sendSourceB=(Button)findViewById(R.id.btn_sendB);
sendDestinationB=(Button)findViewById(R.id.btn_sendDataBa);
activity=getClass().getSimpleName();
sendDestinationB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent senda= new Intent(ActivityB.this,ActivityC.class);
senda.putExtra("destination",src);
senda.putExtra("Activity",activity);
startActivity(senda);
}
});
sendSourceB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent send= new Intent(ActivityB.this,ActivityC.class);
send.putExtra("source",src);
send.putExtra("Activity",activity);
startActivity(send);
}
});}}
now how to check in activityC which activity is calling this activity and which buttonclicklistener is calling the intent
You need to send the value for determine what value and what activity via Intent.putExtra(). Please be remember that you need to set the key as the first parameter for Intent.putExtra(), like
intent.putExtra(THIS_IS_THE_KEY, THIS_IS_YOUR_VALUE);
You need to create something like this:
// This is the key for your putExtra
// you need to create this as global variable.
public static final String FROM_KEY = "FROM";
public static final String ACTIVITY_KEY = "ACTIVITY";
public static final boolean IS_FROM_SOURCE = true;
// This is a sample to send data to Activity C
// where the activity caller is B and from source
Intent senda= new Intent(ActivityB.this,ActivityC.class);
senda.putExtra(FROM_KEY, IS_FROM_SOURCE);
senda.putExtra(ACTIVITY_KEY,"activity_a");
Then in your Activity C, you need to receive the Intent Extra.
You can get the value in Activity onCreate(), something like this:
Bundle extras = getIntent().getExtras();
boolean from = extras.getBoolean(FROM_KEY);
String act = extras.getString(ACTIVITY_KEY);
// do something here if from activity a
if(act.equals("activity_a")) {
if(IS_FROM_SOURCE) {
// do something if from source
} else {
// do something if from destination.
}
} else { // if from activity a
if(IS_FROM_SOURCE) {
// do something if from source
} else {
// do something if from destination.
}
}
In onCreate or anytime after that method is called in Activity-C, you should do the following:
Intent intent = getIntent();
if (intent != null) {
String activity = intent.getStringExtra("Activity");
String src = intent.getStringExtra("source");
// Do something with those values
}
How do I pass data between these activities because now I can play music in first activity when I click the list item,but now I want to pass data from first activity which contains list to second activity directly play song in second activity and also I am confuse about service because I am using service in my first activity when I play song in second activity what will be happen on service
You can do it adding extra data to the intent that starts 2nd activity.
For that your song class must implement Parcelable
public class Song implements Parcelable
{
string artist;
string songName;
string filePath;
#Override
public int describeContents()
{
return 0;
}
protected Song(Parcel in)
{
artist = in.readString();
songName = in.readString();
filePath = in.readString();
}
#Override
public void writeToParcel(Parcel dest, int flags)
{
dest.writeString(artist);
dest.writeString(songName);
dest.writeString(filePath);
}
}
And then when opening second activity to play song it would be like this:
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
intent.putExtra("song", songToPlay);
getApplicationContext().startActivity(intent);
For last you would receive the song in the 2nd activity as follows:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.song_player);
Intent intent = getIntent();
if (intent != null)
{
if (intent.hasExtra("song"))
{
Song songToPlay = intent.getExtra("song");
}
}
}
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 !
I have a class that implements the Parcelable class. From main activity, I pass an parcelable object. At the second activity I retrieve the objet and use the objet to change data. All is ok. But the problem is when I need to retrieve this changes from second activity to the main activity. I don't know how could I do it.
Here the call in the main activity:
public class MainActivity extends Activity {
Tgestion Tges= new Tgestion();
Button buttonI,buttonM,buttonB,buttonD,buttonS;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
buttonI = (Button) findViewById(R.id.buttonIntroducir);
buttonI.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, IntroducirPatron.class);
intent.putExtra("com.example.sistemacontrasena.gestion", Tges);
startActivity(intent);
}
});
}
}
Tges is an objet that implements the parcelable class.
In the second activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_introducir_patron);
// --- Obtenemos objeto Parcelable desde el Intent
this.gestion=getIntent().getParcelableExtra("com.example.sistemacontrasena.gestion");
}
After this I set some changes for example this.gestion.setSecret(value);
When I close this second activity, in the main activity I don't know how can I retrieve this.gestion.getSecret() with the value that I put, for example. How can I do that?
The class parcelable is:
import android.os.Parcel;
import android.os.Parcelable;
public class Tgestion implements Parcelable{
private String[] secret;
public Tgestion(){
this.secret=new String[2];
}
/**
* Constructor Tgestion para parcel.
* #param source
*/
private Tgestion(Parcel source) {
this.secret=new String[2];
readFromParcel(source);
}
public void setSecret(String[] s){
for (int i=0;i<this.secret.length;i++){
this.secret[i]=s[i];
}
}
public String[] getSecret(){
return this.secret;
}
#Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
#Override
public void writeToParcel(Parcel destino, int flags) {
// TODO Auto-generated method stub
destino.writeStringArray(this.secret);
}
private void readFromParcel(Parcel in){
in.readStringArray(this.secret);
}
public static final Parcelable.Creator<Tgestion> CREATOR = new Parcelable.Creator<Tgestion>() {
#Override
public Tgestion createFromParcel(Parcel source) {
return new Tgestion(source);
}
#Override
public Tgestion[] newArray(int size) {
return new Tgestion[size];
}
};
}
You cannot just change the Parcelable and expect the first activity to see these changes. Parcelables are serialized and deserialized, which means that you have a new copy.
You should use startActivityForResult(), setResult() (in the second activity) and onActivityResult() (in the first one) to return data.
See Getting a Result from an Activity in the docs.