Android, start new activity using same activity class, is this possible? - android

i want to create an app with navigation, wiyh navigation flow like this
ListView Activity -> Detail Activity (item 1) -> Detail Activity (item 2)-> Detail Activity (item 3) -> ... and so on
is this posible? how to achive this?
Edited
Sorry for being not clear,
Lets say i have activity with ListView, then when i tap one item, it will start new activity which contain detail information of selected item, and from those activity, there are next / previous button, and when i tap it, it will start new activity using same class and layout, but with information of next or previous item of ListView previously selected item.
Oh and also I need that user can return to previous screen on each backkey, so when user tap back key the activity will go like this
Detail Activity (item 3) (finish) -> Detail Activity (item 2) (finish) -> Detail Activity (item 1) (finish) -> ListView Activity

Do this way
Yes this is possible to call Activity call it self, because Every activity is different process in android.
Activity stack will maintain by Android Don't worry about that.
public class ListActivity extends Activity {
// call DetailAcitvity
Intent intent = new Intent(this, DetailActivity.class);
intent.putExtra("item", "item1");
startActivity(intent);
}
public class DetailActivity extends Activity {
// Calling itself
Intent intent = new Intent(this, DetailActivity.class);
intent.putExtra("item", "item2");
startActivity(intent);
}

Related

back stack keeps adding when using Intents

I have an activity A which uses a fragment A which has a list. The activity A can call on a search activity. The problem that I am seeing is if I were to go the search activity and then go back to the activity A, from there fragment A gets loaded. If I select an item from Fragment A i get taken to Fragment B, if i were to press the back button i have to click it 2 or 3 times. Any ideas? Do i need to start the search activity with a parameter so it does not add to the back stack. I have tried the Flag to Intent.FLAG_ACTIVITY_CLEAR_TOP when starting the search activity but the issue occurs.
Java:
Activity A:
public void popFragment(){
if(getSupportFragmentManager().getBackStackEntryCount() > 1) {
getSupportFragmentManager().popBackStack();
getSupportFragmentManager().executePendingTransactions();
}
}
Fragment A:
private void showSearchActivity(){
Intent intent = new Intent(context, SearchActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivityForResult(intent, 0)
}

Android Activity A B C, switching from C to B with intents,

I have an android application with three activities the flow is like this
A->B->C
The problem is when i go from Activity C to Activity B. ( B<-C)
DETAILS AND BACKGROUND:
I Have an arraylist of of Albums that contain a list of photos, that contain a list of tags. Activity A deserializes the ArrayList to see if there are any new changes in the ArrayList. Activity A handles adding new albums, after adding albums I serialize the ArrayList, this works. I select an album which takes me to Activity B(Arraylist gets passed to activity B, successfully). In this Activity(B) I can add images. After adding images i serialize the Arraylist. This works, i can switch between activities A and B and the photos get saved. When I select the back button on activity B it takes me to Activity A, I do so by doing the following
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
case R.id.action_add:
.
.
.
case:
etc,
.
.
}
}
(EACH activity has the android back button in the top left hand corner)
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
In doing so I learned control flow goes into Oncreate() inside of Activity A as though a new instance is being created?
We are now inside of Activity B
Now If I select an image it takes me to Activity C.In this Activity(C) I can add multiple Tags. I serialize the ArrayList, then i click the back button taking me to Activity B, now that i am in activity B I click the same photo which then takes me to Activity C, but the tags are not displayed associated to the image, If i hard code tags they show up. But it seems as though the arraylist never gets updated. I don't know where control flow goes Back inside of Activity B, If i choose to take the path with OnCreate() A new instance gets created making my list object null
intent = getIntent();
arrayList = (ArrayList)intent.getSerializableExtra("key1");
becomes null, Since a new instance of Activity B gets created, getting invoked By C ,this ArrayList never gets passed to Activity B, because only Activity A calls B in that fashion right?
I want Activity C to be able to save the Arraylist properly, When i click the back button taking me to activity B, and re click the same photo i expect to see the tags pertaining to the image
Activity A:
Calls activity B by Intent
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("key1", list); //list is an ArrayList
I pass a list to activity B
Activity B:
control flow then goes into Oncreate();
This method OnCreate() contains
intent = getIntent();
arrayList = (ArrayList)intent.getSerializableExtra("key1");
I receive the arraylist succesfully.
now I want to pass the list to Activity C
Intent intent = new Intent(SecondActivity.this, ThirdActivity.class);
intent.putExtra("key2", list);
Activity C
{
control flow then goes into Oncreate();
This method OnCreate() contains
intent = getIntent();
arrayList = (ArrayList)intent.getSerializableExtra("key2");
now when i press the back button, i do so by doing this
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);//control goes to onCreate()??
case R.id.action_add:
.
.
.
case:
etc,
.
.
}
}
Now lets take away NavUtils.NavigateUpFromSameTask(this);
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case android.R.id.home:
//NavUtils.navigateUpFromSameTask(this);
finish();
case R.id.action_add:
.
.
.
case:
etc,
.
.
}
}
In doing so I get back to Activity B WITHOUT entering onCreate(), but if i click the photo the tags don't show up.
}
Still inside of activiy C

Get reference to parent activity

From Child Activity, I need to get next item in the ListView of the parent Activity.
Something like below should be perfect(not working, just exemple)
ParentActivity parent= (ParentActivity)getCallingActivity();
ArrayList yeahIGotReferenceToParentArray = parent.array;
Activity A= Listview with 30 row.
When user Click on a row, it open a new Activity B.
From Activity B, I have a button "Next", to get the next item of the ListView on activity A.
Possible solution:
I can use a Intent to provide the full array of the listView, but a better approach can Be to get the reference to the Activity A (from activity B), so I can access value.
You could pass that data, while you are still in activity A, to activity B through an intent. So in your onClick() method you could do something like...
Intent i = new Intent(ActivityA.this, ActivityB.class);
i.putExtra("key", "value");
startActivity(i);
finish();
and in ActivityB
Bundle extras = getExtras();
if(extras != null){
if(extras.containsKey("key")){
//do something based on what the value of the data is
}
}
I don't know if this is the most efficient way of doing this but it could work!

how to refresh fragment

I have an activity where on the last fragment there is a button to send the user to ACTION_INPUT_METHOD_SETTINGS. Then when the user presses the back button, it's still my last fragment. I make the INPUT_METHOD_SERVICE appear, but when something is selected on that dialog, I want to reload my activity. I know how to reload the activity but I don't know how I can detect when something is selected on the INPUT_METHOD_SERVICE.
This is the way I read my activity in onResume:
Globals.securityWizardPage = 4;
Intent myIntent = new Intent(v.getContext(), SecurityWizardActivity.class);
startActivity(myIntent);
getActivity().finish();

load activity with a different state than the default one

I have two activities in my application. Activity1 has a list view and two buttons(say button1 and button2). Based on which button user clicks the content in the listview changes accordingly. The default loading of activity is having button1 click contents loaded in activity1.
In my Activity2, I have a button(say button3) which when clicked, has to load activity1 but with the listview loaded with button2 click results, not with the default display which shows the button1 click results.
Any help on how can I acheive this? On the onclick event of button3 in activity2 I can load activity1 but that will load with the default state showing button1 click results.
Save the state of your ListView by passing a extra string with IntentHandler when switching between the activities:
Intent intent = new Intent(getBaseContext(), yourActivity.class);
intent.putExtra("LISTVIEW_STATE", myListViewState);
startActivity(intent);
Then in your recieving activities onCreate() method (in this case yourActivity) you can get the state with:
Intent intent = getIntent();
String recievedListViewState = intent.getStringExtra("LISTVIEW_STATE");
From second activity send Intent with a flag.
In first activity always lookout for this flag and trigger your second button code accordingly. For reference you can search for "passing data between activity" here or google.
You can do this by adding Extras on your Intents. Right now, when you start Activity1 from Activity2, you probably have code that looks something like this:
Intent intent = new Intent(Activity2.this, Activity1.class);
startActivity(intent);
You can use the putExtra() method on the Intent that sets a key-value pair (called an extra) on this intent, making it look like this.
Intent intent = new Intent(Activity2.this, Activity1.class);
intent.putExtra("LIST_TO_DISPLAY", "LIST_2");
startActivity(intent);
The first argument to putExtra() (the key) is always a String, the second argument (the value) can be many different data types. Here's it's also a String.
Once Activity1 starts, you can grab the Intent by using getIntent() then pull the extra from it, all in onCreate().
protected void onCreate(Bundle b)
{
Intent intent = getIntent();
String whichList = intent.getStringExtra("LIST_TO_DISPLAY"); //which List now equals "LIST_2"
if( whichList != null && "LIST_2".equals(whichList) )
{
//Set up List 2
}
else
{
//Set up the default list
}
}
Here's the docs for the Intent Class. You can find explinations of all of the put and get Extra methods, as well as some other information that may be helpful when wanting to customize how you started Activities behave.
I dont understand so there are several things which would you think.
...
Activity1 -> Button1 -> Listview (Content A)
Activity1 -> Button2 -> ListView (Content B)
Activity1 -> Activity2 -> Button3 -> Activity1 -> ListView (Content B)
So you could
in activity1
start startActivity(intentActivity2);
In activity2
intentResult.putExtra("cmd", "button3");
onbutton click setResult(RESULT_OK,intentresult)
and finish
back in Activity1
onActivityResult
resultIntent.getExtra("cmd")

Categories

Resources