How to manage all activities transference with conductor class - android

I want to manage all activities with class conductor like this:
Also all activities extend base activity to use common view.
In this case, I want to handle transfer activity, for example:
Base -> First -> Second -> Third -> First
Base -> First -> Fourth -> Fifth -> Fourth
When transferring activity, Conductor must handle all activity in stack.
I try to write this conductor as below (I use list to manage instead of stack):
public class Conductor {
private List<Activity> listOfActivityInStack;
public Conductor(){
listOfActivityInStack = new ArrayList<Activity>();
}
public void startActivity(Activity activity, Class<?> cls){
listOfActivityInStack.add(activity);
Intent i = new Intent(activity.getApplicationContext(), cls);
activity.startActivity(i);
}
public void startActivityForResult(Activity activity, Class<?> cls, int requestCode){
listOfActivityInStack.add(activity);
Intent i = new Intent(activity.getApplicationContext(), cls);
activity.startActivityForResult(i, requestCode);
}
public void startAcitivtyClearPrevious(Activity activity, Class<?> cls){
listOfActivityInStack.clear();
listOfActivityInStack.add(activity);
Intent i = new Intent(activity.getApplicationContext(), cls);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
activity.startActivity(i);
}
public int getCount(){
if(listOfActivityInStack == null)
return 0;
return listOfActivityInStack.size();
}
}
I store this conductor in Global variable. Then I use it as below:
//Get conductor from application global
conductor.startActivity(FirstActivity.this, SecondActivity.class);
//Then add conductor to application global
But I have some problem:
I must handle goBack() for all activity to remove activity from list.
Check activity has exist in list, if yes, try get its instance.
Is there best way to manage all activity on android? I have tried search but not found good answer. I wonder weather or not my way is right. Any recommend or example would be help!

there is one way to do this. you must save your all activities state. and when you need to recall them you must use that state.
for extra info look here:
Saving Android Activity state using Save Instance State

Related

pass data from activity one to activity three directly while going through activity two but data should not be passed to activity two

I have three activities A,B and C. I need to pass some data from activity A to activity C. But the navigation is from A to B to C i.e. I can't go to activity C from activity A directly. I don't want to pass my data to activity B from A and I don't want to use any external storage like sqlite or shared preferences. Can it be done through intent ? If yes , how ? If no, is there any other way ?
use Interface to communicate with other Activity.
or you can make your variable public static on Activity1
The problem is that your third activity has no way of knowing which activity sent the bundle. You need to add a field that identifies what type of bundle this is so you can process accordingly. For instance, in activity 1:
Intent i = new Intent(ActivityOne.this, ActivityTwo.class);
i.putExtra("activity", (int)1);
i.putExtra("key", value);
startActivity(i);
Then in 3rd activity:
Bundle extras = getIntent().getExtras();
if(extras !=null) {
int typeAct = extras.getInt("activity");
if (typeAct == 1) {
//do something with data
}
There are various ways to send the data from one activity to other activity,
Now as you can not use intent so I would suggest you to go with either Application class or create a Global singleton class to hold your data.
Please refer following link
https://androidresearch.wordpress.com/2012/03/22/defining-global-variables-in-android/
Following are the steps to create an application class:
Step 1: Create any class and extend it with the Application class:
public class Globals extends Application{
private int data=200;
public int getData(){
return this.data;
}
public void setData(int d){
this.data=d;
}
}
Step 2: Defining the application class in the manifest file:
<application
android:name=".Globals"
.... />
Step 3:Now by calling getApplication() in any of the activity you get your application class. Just typecast it and call the methods required.
Globals g = (Globals)getApplication();
int data=g.getData();
I hope this will help.
make one interface :
public interface PassData {
public void data(String data);
}
if you want to pass the data from activity A to activity C then :
from Activity A you need to pass the data like this:then on Activity A.
PassData passData;// make it global variable
passData.data("Your DATA WHICH YOU WANT TO SEND TO ACTIVITY C");
In Activity C :
ActivityC extends AppCompatActivity implements PassData{
//onCreate Stuff
#Override
public void data(String data) {
// print this data and see it's coming or not
}
yes sure.
for example in your Activity
class ActivityA extend Activity{
public static String abc = "hello";
}
in ActivityC
ActivityC extend Activity{
onCreate(){
String a = ActivityA.abc;
Log.d("test",a);
}
}

how to pass activities dynamically in intent to navigate b/w activities in android?

I am trying to create a common method in a non-activity class which will navigate from one activity to another activity. My problem is that I am not able to pass activities dynamically as I have my activities in package "com.example.activity" and the method I want to create is in package "com.exmple.commonmethods". Please tell me what should I do to achieve this.
public static void intentFinish(Activity a, Activity b) {
Intent intent = new Intent(a.getApplicationContext(), b.class);
a.startActivity(intent);
a.finish();
}
Try this one:
public static void StartNewActivity(Activity a, Class<? extends Activity> class1) {
a.startActivity(new Intent(a, class1));
a.finish();
}

Pass value through intent or static variable?

I am wondering about the best way to design / program this:
If I have a Boolean value (let's say whether the user has extra power or not) and I need to pass it from Activity A to B to C. Should I add it to an intent from each activity to another OR should I just store it in a static variable and access it every time?
Its is safer to pass it in the intent. sometimes android kills apps without warning when it needs memory and your static values will not be retained on the other hand intent extras are kept. if you want to push it a little further, use shared preference. its designed using Map data struct so speed will not be a problem.
Android Intents have a putExtra method that you can use to pass variables from one activity to another.
public class ActivityA extends Activity {
...
private void startActivityB() {
Intent intent = new Intent(this, ActivityB.class);
intent.putExtra("HAS EXTRA POWER", false);
startActivity(intent);
}
}
public class ActivityB exntends Activity {
Bundle bundle;
private void hasExtraPower() {
bundle = getIntent().getExtras();
if(!bundle.getBoolean("HAS EXTRA POWER")
// do something
else
// do something else
}
}
Passing data through Intent
If you use that only in that activity that's fine but
When u need to pass to other layer like viewmodel that will make your operation's speed slower

How to pass interface to constructor when creating an Intent

I have two Activities: the MainActivity starts the NewReminderActivity. The first one will be notified when a new reminder has been created. Therefore it implements the interface OnEventAddedListener.
Do I need to use serialization to add the MainActivity to the intent or is there a better solution? I've never seen any examples using serialization to accomplish this and I'm sure it's very common to pass an interface from one activity to another in order to communicate.
public class MainActivity extends Activity implements OnEventAddedListener {
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
if(item.getItemId() == R.id.action_addReminder)
{
// NewReminderActivity c = new NewReminderActivity(this);
// Intent intent = new Intent(this, c.getClass()); // this won't work
Intent intent = new Intent(this, NewReminderActivity.class);
startActivity(intent);
return true;
} else {
return super.onOptionsItemSelected(item);
}
}
}
You absolutely should not try to pass one activity to another, whether it's by serializing it (which won't even work for a number of reasons) or setting a reference.
Android will take care of cleaning up old activities out of memory, but won't be able to do so as long as you're holding on to a reference from it. Never hold on to other activities or fragments outside of their context!
You should follow the documentation on starting activities and getting results by using startActivityForResult() and provide that activity's result through onActivityResult(int, int, Intent).

Accessing instance of the parent activity?

Suppose I have a class first.java (activity class) and I start another activity in this class (second.java - activity class).
How can I access the instance of first.java from second.java?
Can someone give me a good explanation on this... An example would be great...
If you need your second activity to return some data to your first activity I recommend you use startActivityForResult() to start your second activity. Then in onResult() in your first activity you can do the work needed.
In First.java where you start Second.java:
Intent intent = new Intent(this, Second.class);
int requestCode = 1; // Or some number you choose
startActivityForResult(intent, requestCode);
The result method:
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
// Collect data from the intent and use it
String value = data.getString("someValue");
}
In Second.java:
Intent intent = new Intent();
intent.putExtra("someValue", "data");
setResult(RESULT_OK, intent);
finish();
If you do not wish to wait for the Second activity to end before you do some work in the First activity, you could instead send a broadcast which the First activity reacts to.
You can simply call getParent() from the child activity.
I have no clue why other answers are so complicated.
Only this should work
class first
{
public static first instance;
oncreate()
{
instance = this;
}
}
first.instance is the required thing that is accessible from the second class
try this if this work 4 u.........
something like this.....
class first
{
public static first instance;
oncreate()
{
instance=this;
}
public static getInstance()
{
return instance;
}
}
now from second class call first.getInstance();
you can also directly acess instance in static way like this first.instance.......
Thanks...
You can't create an activity directly.
In the first activity take a static activity variable like this,
public static Activity activity;
In the onCreate do this.
activity = this;
Then in the second activity do this,
Activity activity = (your activity name).activity;
Edit:
For passing data from one activity to other activity this is not the way.
Above answer was to get activity instance from other activity which was initially asked.
To pass data from one activity to other activty generally use bundle. But if the data is not primitive data type, then use object class which should implement parcelable or serializable interface. Then through bundle only parcelable list of objects we can pass.

Categories

Resources