Same as my last question, I'm building custom contacts app which saved the contacts on the app only.
I've encountered an issue from creating a contact and viewing it.
Whenever I click on "OK" on the new contact screen, instead of opening the activity with the input, I'm being pushed back to contact list.
I've seen countless of questions here about problem passing information from activity to another, yet it didn't help me.
I've switched from startActivity(intent) to startActivityForResult(moverIntent,1);
I've tried using putExtra() to each string instead of bundle.
I've tried to send only 1 string instead of 4 and still didn't work.
The AddNewActivity.java:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addcontact);
ok=findViewById(R.id.okIB);
not_ok=findViewById(R.id.notokIB);
fullName=findViewById(R.id.fullnameET);
nickName=findViewById(R.id.nicknameET);
email=findViewById(R.id.emailET);
phoneNum=findViewById(R.id.phoneET);
final String name,nick,emaill,phone;
name=fullName.getText().toString();
nick=nickName.getText().toString();
emaill=email.getText().toString();
phone=phoneNum.getText().toString();
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ok.setImageResource(R.drawable.okpress);
Intent moverIntent=new Intent(AddNewActivity.this,ContactActivity.class);
Bundle bundle=new Bundle();
bundle.putString("fullName",name);
bundle.putString("nickName",nick);
bundle.putString("email",emaill);
bundle.putString("phoneNum",phone);
moverIntent.putExtras(bundle);
startActivityForResult(moverIntent,1);
}
});
The ContactActivity.java:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact);
//Intent get_info=getIntent(); //Previous attempt of getting the Bundle
Bundle old_info= getIntent().getExtras();
final String phone_num,full_name,nick_name,email_add;
phone_num=old_info.getString("phoneNum");
full_name=old_info.getString("fullName");
nick_name=old_info.getString("nickName");
email_add=old_info.getString("email");
pic_btn=findViewById(R.id.take_pic);
date_btn=findViewById(R.id.take_date);
call_btn=findViewById(R.id.take_call);
sms_btn=findViewById(R.id.send_sms);
phone=findViewById(R.id.contact_phoneTV);
name=findViewById(R.id.contact_nameTV);
nick=findViewById(R.id.nick_nameTV);
email=findViewById(R.id.emailTV);
phone.setText(phone_num);
name.setText(full_name);
nick.setText(nick_name);
email.setText(email_add);
Based on what I've learned and read here, I have expected that the activity ContactActivity to be open and display all the information from the AddNewActivity, what does happen is that I'm being thrown back to the list activity (didn't post it here).
Related
I got an assignment to create an application. At the Splash screen stage, it is instructed to get a list from SQLite while in a Splash screen, and pass it to a Fragment (I did so by using an Intent).
My question is why not get it from the Fragment rather than passing it from the Splash screen to the Main Activity, and from there to the Fragment? It may seem unnecessary if it wasn't for some reason that's unknown to me.
When looking for information on this question I couldn't find anything. I guess it didn't come up previously, or at least I couldn't find the phrasing that was previously used.
The method that gets the list and passes it to the MainActivity:
private void toMainActivity(ArrayList<Movie> moviesList) {
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
intent.putParcelableArrayListExtra("moviesList", moviesList);
startActivity(intent);
finish();
}
At MainActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<Movie> moviesList = this.getIntent().getParcelableArrayListExtra("moviesList");
Bundle listBundle = new Bundle();
listBundle.putParcelableArrayList("moviesList", moviesList);
Fragment mlf = new MoviesListFragment();
mlf.setArguments(listBundle);
ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.fragments_container, mlf);
ft.commit();
}
At the Fragment:
Bundle listBundle = getArguments();
if (listBundle != null) {
moviesList = getArguments().getParcelableArrayList("moviesList");
moviesAdapter.attachMoviesList(moviesList);
}
That's usually a matter of making sure you're working on the same instance of something. When passing something in a Bundle you're securing that the next screen will work on the same instance of the passed data that the previous screen was.
Whether it's better or worse than querying a db every time is a case-by-case thing. Sometimes you want to maintain data "continuity" between screens and sometimes you want to get the newest copy of the data (which could've been modified asynchronously).
I have an activity which can be accessed via different activities.
Like you have one activity containing a listview and the second containing a gridview and both shows the same data. When you click on an item a new activity with some details is shown. I need to somehow remember which activity was the initial one (with gridview or listview) so that I can set a button to redirect here. But it's not enough to just return to previous activity (like using finish() to close the current one), because there is a way to navigate among different objects from inside the details activity (I have a gridview on that screen). So I need to remember the initial view during moving through the details activity for various number of times.
Is there a way?
EDIT: omg why so many downvotes? At least tell me why it is so stupid, I'm learning coding for Android for 2 weeks how am I supposed to know everything??
This sounds like it would best be solved by using two Fragments within the same Activity
You can use a bundle object to pass data to the new activity (B) so you could know wich activity (listView or gridView) had started it.
In activity B:
private static final String ACTIVITY_TYPE = "activity_type";
public static Intent createIntent(Context context,String activityType)
{
Intent intent = new Intent(context, ActivityB.class);
Bundle b = new Bundle();
b.putString(ACTIVITY_TYPE,activityType);
intent.putExtras(b);
return intent;
}
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Bundle b = getIntent().getExtras();
String activityType;
if (b != null)
{
activityType = b.getString(ACTIVITY_TYPE);
}
//the calling activity didn't use the createIntent method and didn't provide the activity type
else
{
activityType = "some_default_type";
}
//save activity type to use later
//rest of your code
}
In the calling activity:
Intent intent = ActivityB.createIntent(ActivityListView.this,"activity_list_view");
startActivity(intent);
If im staying in the app, this works fine
Click button to take me to new activity:
intent.putExtra("invite_id", invite_id);
startActivity(intent);
Receiving Activity:
Bundle b = getIntent().getExtras(); //invite id is in here
Now here is the weird part. If I am in the app, then click home button to leave the app and go to the native contacts app and save ANYTHING (like edit a name or number...the problem only occurs if I actually save something), then go to recent apps and open up my app from there... now if I click the button to launch my intent to take me to a new activity, the receiving activity returns a null bundle
Bundle b = getIntent().getExtras(); //returns null
Why could this be happening?
String b = getIntent().getStringExtra("invite_id");
Intent extras are always persisted across activity death and recreation. So, if you're stashing that extra value, it will continue to be there if you are resuming the app with the recent app switcher.
I would verify that you are stashing values in the intent.
simply that means that your activity being recreated so you have to options to avoid that:
Avoid recreating your activity by setting this to your Activity within the manifest
android:configChanges="keyboardHidden|orientation|screenSize|locale"
Or by saving the instance of the id you received by doing such:
#Override protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("myId",myId);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
if (savedInstanceState == null) {
myId = getIntent().getExtras().getString("myId");
}
}
Hi everyone.
I'm new in android and I'm working on an app in which I need to recall the same activity to enter the information of a variable amount of entities (passengers).
I have the following:
btnContinue3.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
for (int i=0; i<Pssngr; i++){
passenger[i] = new
Intent(getApplicationContext(), Pasajeros.class);
startActivity(passenger[i]);
}
}
});
Pssngr is the amount of passsengers or entites that need a unique activity to get their information entered.
The trigger is the button then the activities will be called one by one following an array
I try this code but after clicking on the button the app crashed.
Please someone help me find a way to make this work.
thanks
It crashes because You are trying to start x number of Activities at once.
If You have to run new Activity for each of Passengers best in this scenario will be startActivityForResult
I beliver effect You trying to get is that user clicks on button just once and activities for each passenger will open one after another.
To do it in method onClick You will start only first activity, don't use loop.
You start consequently next activities in onActivitiyResult
In addition to what Gustek mentions above, a better way to approach this would be to have one activity and just pass the different values from the parent (PassengersAvitivty) activity through the intent as below:
final Intent intent = new Intent(PassengersAcitivty.this, PassengersEntityActivity.class);
intent.putExtra("PASSENGER_FIRSTNAME", passenger[i].getName());
intent.putExtra("PASSENGER_LASTNAME", passenger[i].getLastName());
intent.putExtra("PASSENGER_EMAIL", passenger[i].getEmail());
startActivity(intent);
and here is how you can retrieve them on your activity (PassengerEntityActivity)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
if (extras != null)
{
firstname = extras.getString("PASSENGER_FIRSTNAME");
lastname = extras.getString("PASSENGER_LASTNAME");
email = extras.getString("PASSENGER_EMAIL");
}
else
{
//Log.v("NO VALUE", "OOPS");
}
I can clarify further if needed.
new to droid programming. im having a small problem that im sure is simply fixed but ive done some searching and a bunch of tutorials but cant seem to find just what i need so i figured id ask. My app has 2 activites, the first activity is just a simple form where a user enters course information(class title, professor..etc.)
the first activity passes the data which is supposed to be stored in a list in the second activity. problem is that only the first course gets stored in the list, after the first time nothing new gets added to the second activity. Can someone point me in the right direction please? thanks in advance
First Activity
public class CourseDetail extends Activity {
//Course c = new Course();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button save=(Button)findViewById(R.id.save);
save.setOnClickListener(onSave);
}
private View.OnClickListener onSave=new View.OnClickListener() {
public void onClick(View v) {
EditText course=(EditText)findViewById(R.id.course);
EditText professor=(EditText)findViewById(R.id.professor);
EditText location=(EditText)findViewById(R.id.location);
EditText officehrs=(EditText)findViewById(R.id.officehrs);
Intent i=new Intent(CourseDetail.this, CourseList.class);
i.putExtra("myCourse", course.getText().toString());
i.putExtra("myProfessor", professor.getText().toString());
i.putExtra("myLocation", location.getText().toString());
i.putExtra("myOfficehrs", officehrs.getText().toString());
startActivity(i);
}
};
}
Second Activity
public class CourseList extends Activity {
Button btnCourse;
List<Course> model = new ArrayList<Course>();
CourseAdapter adapter=null;
private String dCourse="";
private String dProfessor="";
private String dLocation="";
private String dOfficehrs="";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.clist);
ListView list =(ListView)findViewById(R.id.courses);
adapter=new CourseAdapter();
list.setAdapter(adapter);
Course c = new Course();
Bundle extras = getIntent().getExtras();
dCourse = extras !=null ? extras.getString("myCourse") :"no value entered";
dProfessor = extras !=null ? extras.getString("myProfessor") :"no value entered";
dLocation = extras !=null ? extras.getString("myLocation") :"no value entered";
dOfficehrs = extras !=null ? extras.getString("myOfficehrs") :"no value entered";
c.setCourse(dCourse);
c.setProfessor(dProfessor);
c.setLocation(dLocation);
c.setOfficeHrs(dOfficehrs);
btnCourse =(Button)findViewById(R.id.btnCourse);
btnCourse.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
You are just getting the user entered value in CourseDetail activity and displaying the received value inside the CourseList activity, that means you are not storing these values permanently.
Go through this Android - Data Storage document.
When you move to 2nd activity i.e. CourseList activity, at that time fetch the data from the SQLite table and display the same. whenever you get new values from previous activity, at that time just update the list by adding the new data in ArrayList and make a call on adapter.notifyDataSetChanged()
Some suggestions:
Have your CourseList extend ListActivity instead of just Activity - check out some tutorials on that which should help you set things up correctly.
There seems to be a bit of confusion with how you're handling your lists - you have your model variable but don't seem to be doing anything with it. Again, have a look at a ListView tutorial (just google "android listview tutorial").
You seem to have figured out that you can use "intents" to pass information from one activity to another, but since you're only doing this in the onCreate() method, it's only happening once. Try doing this in your ListActivity's adapter once for each item.
Don't give up on Android, keep trying :-)
Some suggestion:
You have to add your object to the adapter: adapter.add(c); after you get the data.
Call adapter.notifyDataSetChanged() to notify the system that your data for the listView has been changed. Call list.invalidate() to refresh it.
I noticed that you set the button with the finish() method. Hmm, if you do so, the next time you get to CourseList Activity from CourseDetail, the adapter will be null again. No previously received data will be available. Is this what you really want?
The problem is you are not adding the newly added items to the List.So before setting adapter you have to add all your objects like
list.add(c);