Using life cycles in android - android

I'm a new android programming beginner :) .
I have made a simple app which adding places .
1- Main activity include buttons{Places , Add place , Favorites Places }
so let's assume we are in the "Add place" activity , we type tite and description and add the place to arrayList, and move on to Places which has this list ..
The problem is when I type back, I get back the details I wrote about the place, but I'm trying to clear it when I press back in the phone ...
how can I do this to avoid crashes ?
Thanks all
-- Update :
Thanks to all people who reply trying to help me, that's awesome :)
I just added :
#Override
public void onBackPressed(){
Intent intent = new Intent(PlacesActivity.this,MainActivity.class);
startActivity(intent);
}
This solution was suggested by : Arsen Sench :)
in the activity that I don't want to press back,

You can do one thing here.
Before jumping to another activity View Places , just clear all the edittext there.
edittext.setText("");
Try this. Will help you.

just clear the EditText field before you start a new activity
editTextTitle.setText("");
editTextDescription.setText("");
intent.startActivity();

You can use startActivity() from both classes. Or you can use startActivityForResult(). Here is example of using startActivity() from both activites.
From first Activity
Intent i = new Intent(YOURFIRSTACTIVITY.this,YOURSECONDACTIVITY.class);
startactivity(i);
finish();
From Second Activity
#Override
public void onBackPressed()
{
Intent i = new Intent(YOURSECONDACTIVITY.this,YOURFIRSTACTIVITY.class);
startactivity(i);
finish();
}

Related

Loose coupling and starting activities

I want to keep my app as loosely coupled as possible, and most of is done with IoC
however, at some point, i need to launch different activities,and the class implementing this activity, could be any,meaning i don't want to define a specific class that starts the activity, but one needs to be set in the intent.
where is the best place to write the code to launch my activities ? is it in the same activity that starts the other activity? or have some outside logic about it?
I have an activity A
from which i need to start activity B
where do i put the logic of
Intent intent = new Intent(this, B.class);
startActivityForResult(intent, requestingB);
It sounds like you're trying to have an activity that can be launched by some other application and you don't want the activity to necessarily know about what is launching it.
Try using an intent-filter within your activity. Then, when something needs to launch it, all it has to do is fire off an intent with the action defined in the intent-filter.
As always, Vogella has a good tutorial here: http://www.vogella.com/tutorials/AndroidIntent/article.html
As the OP mentioned in comments that he wants to start another activity on the click of a button, below is the sample code:
Button myBut = (Button) findViewById(R.id.but1);
myBut.setOnClickListener(new onClickListener()
{
#override
public void onClick(View view)
{
Intent intent = new Intent(A.this, B.class);
startActivity(intent);
}
});
Hope this helps

Reload or Refresh Activity from OnClickListener not working

In my "ArticleActivity" (where the user reads the article), I have a list of "related articles". When a user clicks one, it should reload or refresh the ArticleActivity to show the article they clicked on instead of the one they just read.
I found many many answers online and have tried roughly 492 of them... I CAN get it to start a DIFFERENT activity, but I can't get it to restart the current one.
My latest attempt:
//RELATED ARTICLE CLICK
relatedArticleClickListener = new OnClickListener()
{
public void onClick(View v) {
Log.d("MYLOG", "related article clicked " + v.getId());
Intent myIntent = new Intent(v.getContext(), ArticleActivity.class);
myIntent.putExtra("id", v.getId());
startActivity(myIntent);
finish();
}
};
Update:
Could have it to do with the ArticleActivity having this: android:launchMode="singleTask" ? I need it there, but also need to be able to reload the activity with a new article.
If I change ArticleActivity.class to MainActivity.class and leave all the rest of the code exactly the same, it DOES go to the MainActivity.
replace these lines
finish();
startActivity(myIntent);
with these
startActivity(myIntent);
finish();
After reading your update, and further understanding your question. The behavior is related to your singleTask designation. Under singleTask, if the activity already exists, it is not recreated, but instead its onNewIntent() method is invoked for you to handle the new intent. See Activity.launchmode for more info.
Do you really need singleTask? Because removing it should also cause your problem to go away. It will also allow your read articles to be in the back stack, so when your user is done with one article and presses the back key it will take them back to the first article.

Refresh activity and re-open

This is my first post, so please be nice :)
I have a question and no one gave the answer in the post I've seen.
My app has a list and a button to open an activity, this activity creates a new item to show in the list of the previous activity when pressing the button Create.
How can I do this?
This is the code I made for the first button:
intent = new Intent(this.getBaseContext(), NewTestActivity.class);
this.finish();
startActivity(intent);
and this is the code to go back an refresh:
Intent intent = new Intent(getBaseContext(), TestListActivity.class);
startActivity(intent);
But the code to goback is useful, because the activity don't refresh.
I have to call the new activity in a diferent way? Or go back to previus activity in a diferent way? Or go back normally and refresh the activity when I'm back in the previus activity?
Well...this is all.
Sorry for my bad english and, if this question has been answered in another thread, give me the link to read, because I can't find it :)
PS: I started with android in December.
Thanks for your help.
When you are going to start new activity you shouldn't close a current one until you actually need this behaviour. (remove this.finish(); row from your code)
Also you shouldn't close an active activity manually until you actually need it. When the user presses the "back" button on the device Android pops the previous activity from the "back stack". Read once more Activity documentation.
According to your description you have a list of elements. So in order to refresh the list you need to update your dataset and notify the list about it by ListView.notifyDataSetChanged() method call.
Before getting to a real answer, I'd like to show some improvements to your code.
First, when creating an Intent (or when you need a context in general) from an Activity there is no need to call getBaseContext(), you can just use this:
intent = new Intent(this, NewTestActivity.class);
Second, android is good at handling Activities, you do not have to close your first activity manually with finish(). Android will automatically pause or stop your first activity and bring it back when you return to it.
Third, in your case you might want to use startActivityForResult() instead of startActivity() for reasons I will explain below.
This will make your code look like the following:
private static final int MY_REQUEST_CODE = 33487689; //put this at the top of your Activity-class, with any unique value.
intent = new Intent(this, NewTestActivity.class);
startActivityForResult(intent, MY_REQUEST_CODE);
Now, the startActivityForResult() starts an activity and waits for a result from that new Activity. When you call finsih() in the new Activity you will end up in the first Activitys onActivityResult()-method, with data supplied from the new Activty that is now closed:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode != MY_REQUEST_CODE) return; //We got a result from another request, so for this example we can return.
if(resultCode != RESULT_OK) return; //The user aborted the action, so we won't get any data.
//After the above if-statements we know that the activity that gives us a result was requested with the correct request code, and it's action was successful, we can begin extracting the data, which is in the data-intent:
Item item = (Item) data.getSerializableExtra("customData"); //casts the data object to the custom Item-class. This can be any class, as long as it is serializable. There are many other kinds of data that can be put into an intent, but for this example a serializable was used.
itemList.add(item); //This is the list that was specified in onCreate()
//If you use an Adapter, this is the place to call notifyDataSetChanged();
}
For this all to work, we need to do some things in the second Activity:
When the item has been created, we must set a result:
//We begin by packing our item in an Intent (the Item class is an example that is expected to implement Serializable)
Item theCreatedItem; //This is what was created in the activity
Intent data = new Intent();
data.putSerializable(theCreatedItem);
setResult(RESULT_OK, data);
finish();
This should return to the first Activitys onActivityResult()-method with the item, as explained above.

Finishing Multiple Activity with Single Statement?

In android finish(); is used to close the Current activity. In my application i've more than 4 Activities. I want to finish them with one single statement. How can i done this here.
I've tried the System.exit(0); it's not working for me. Why this not working for me also? Anyone guide me here? Thanks in Advance.
Its better to always go back to homeActivity by using startActivity of your home screen. you can find answers here. How to close all the activities of my application? and Is quitting an application frowned upon?. its already discussed here
Use following set of instructions.
Intent intent = new Intent(getApplicationContext(), Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
I've got the answer. I've give the moveTaskToBack(true); to my Button Click event. This works fine. Thanks for you all whose reply me and answer me.

How to navigate to another page in android?

I'm new to android. Please tell me how to navigate to a new page in android.
Thanks in advance.
Edit:How to start a new activity from an existing activity
In android to navigate to another page means you have to start another activity. for starting a new activity use this
Intent intent = new Intent(currentActivity.this, nextActivity.class);
startActivity(intent);
You should mention the next activity in the AndroidManifest file.
To change your page (i think you're refering to "Activities" in android you need to issue a so called "intent").
You cann do that by:
startActivity(new Intent(nextactivity,NextActivity.class));
or
startActivityForResult(new Intent(nextactivity,NextActivity.class),1);
if you want the new Activity to return any result to the activity who started the new one.
But what i recommend is, that you read this documentation here:
http://developer.android.com/guide/topics/intents/intents-filters.html
and come back to this question if you have need additional assistance on concrete problems on that topics.
i hope that helps.

Categories

Resources