How to sending data to a specific activity that is already started? - android

Suppose we have two simple applications, so we have two different packages.
Each of these packages has an Activity that can be launched by clicking the application icon. Suppose that the two activities are as follows:
MyFirstActivity, which is into the package org.firstexample.firstactivity
MySecondActivity, which is into the package org.secondexample.secondactivity
Suppose we have launched the MyFirstActivity activity, so it is running.
Could the MySecondActivity activity send data directly to the MyFirstActivity activity?
I would like the two activities (which are in different packages) can communicate with each other by exchanging data.

if you are launching MySecondActivity from MyFirstActivity then use this way:
in Activity MyFirstActivity:
Intent intent25 = new Intent(Intent.ACTION_MAIN).addCategory(
Intent.CATEGORY_LAUNCHER).setClassName("org.secondexample",
"org.secondexample.MySecondActivity").addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.addFlags(Intent.FLAG_FROM_BACKGROUND).setComponent(new ComponentName("rg.secondexample",
"org.secondexample.MySecondActivity"));
Bundle bundle = new Bundle();
bundle.putString("Name", "test");
intent25.putExtras(bundle);
getApplicationContext().startActivity(intent25);
and in MySecondActivity oncreate()
Bundle bundle = this.getIntent().getExtras();
String name = bundle.getString("Name");

If you want to pass data without resuming on destroying activity then you have to make listener for that..
public class MyFirstActivity extends Activity implements OnDataChanged {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public void onChange(int a) {
Log.e("", "a : " + a);
}
}
MySecondActivity.java
public class MySecondActivity extends Activity {
private OnDataChanged mOnDataChanged;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sendData(10);
}
public interface OnDataChanged {
public void onChange(int a);
}
private void sendData(int a) {
mOnDataChanged.onChange(a);
}
}
MySecondActivity is sending 10 to MyFirstActivity by implementing listener of MySecondActivity...

Related

Custom Objects not being passed to other Activity via intent PutExtra

So I am making an android app that implements some custom classes. I want to create an object of class Menu_Organizer to other activities, but after I inizialize the object and send it to the next Activity the object is NULL. Here are the classes:
Menu Items class
import java.io.Serializable;
public class Menu_Items implements Serializable {
private String Name = "";
private String Description = "";
private float Price = 0.0f;
private String Category = "";
/* Getters and Setters*/
}
Menu Organizer class:
public class Menu_Organizer implements Serializable {
ArrayList<String> Categories;
ArrayList<Menu_Items> Food;
// EDITED
public Menu_Organizer() {
Categories = new ArrayList<String>();
Food = new ArrayList<Menu_Items>();
}
/* Other class methods */
}
First Activity (main)
public class MainActivity extends AppCompatActivity {
private Button btn;
public Menu_Organizer menu;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
onclick();
}
public void onclick() {
btn = (Button) findViewById(R.id. btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Menu_Organizer menu = new Menu_Organizer();
menu.New_Menu_Item("Carne", "Pollo", "Pollo Asado Rico!", 4.55f);
Intent activity2= new Intent(MainActivity.this,temp.class);
activity2.putExtra("Menu", menu);
startActivity(activity2);
}
});
}
}
Second Activity
public class temp extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_Second);
Bundle bundle = getIntent().getExtras();
Menu_Organizer menu = (Menu_Organizer) bundle.getSerializable("Menu");
String str= menu.Food.get(0).getName();
}
}
Alright, i think that the issue is, that when you pass your class object in a key value pair, you do it in an Intent object, but when you resolve your intent, you do that via a bundle object. So, in you temp Activity, you should resolve the intent like:
Intent intent = this.getIntent();
Menu_Organizer menu = (Menu_Organizer) intent.getSerializableExtra("Menu");
Try this, this should work. If you want to do it via a bundle, then create a bundle object first, then put whatever you want in that bundle. Add the bundle to your intent, and then resolve the bundle in your next Activity.
Just a tip, Class names generally do not contain _ in them, use CamelCase naming convention for all classes.
I would recommend using EventBus library for this kind of thing. It is quite easy to use and gives you exactly this: sending and receiving custom object classes from one place to another (Fragments, Activities, Services, whatever you wish can send and receive objects).
I personally don't like intents cause they have too many limitations.

Why are SingleTop activities sometimes created multiple times

I have an activity with a launch mode set to singleTop in the manifest. However if this activity is started multiple times through startActivity called one after another, multiple instances are still created.
Here is the code that demonstrates the behavior. MainActivity displays a single button that start SingleTopActivity twice when clicked. SingleTopActivity displays a message received from MainActivity.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button startActivity = (Button) findViewById(R.id.startActivity);
startActivity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startSingleTopActivity("first");
startSingleTopActivity("second");
}
});
}
private void startSingleTopActivity(#NonNull String message) {
Intent intent = new Intent(this, SingleTopActivity.class);
intent.putExtra(SingleTopActivity.EXTRA_MESSAGE, message);
startActivity(intent);
}
}
public class SingleTopActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "MESSAGE";
private TextView mMessage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_single_top);
getSupportActionBar().setTitle("SingleTopActivity");
mMessage = (TextView) findViewById(R.id.message);
mMessage.setText(getIntent().getStringExtra(EXTRA_MESSAGE));
}
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
String message = intent.getStringExtra(EXTRA_MESSAGE);
mMessage.setText(mMessage.getText() + " " + message);
}
}
When no additional intent flags are present SingleTopActivity with second message is launched. When navigated back SingleTopActivity with first message appears instead of MainActivity.
However if launched in debugger (so starting activity takes longer time due to breakpoints etc.), or when flags like FLAG_ACTIVITY_REORDER_TO_FRONT or FLAG_ACTIVITY_CLEAR_TOP are set only one instance of SingleTopActivity is launched, as expected.
Why does singleTop not work as expected in this case?
As this page says, if a activity is set to singletop, only if the activity is in top of the task, it won't be created again.
In your code,when you call startSingleTopActivity("second");,the top activity of the task is MainActivity not SingleTopActivity, so it will be created twice.

how can android application reboot itself?

MyActivity started by another Activity (different process) with extra string value in intent.
If it reboots itself through MyActivity::restart() like following, extra string value will be received again in MyActivity::onCreate(). That is what I do NOT want.
Is it a wrong way to reboot ?
class MyActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
String value = getIntent().getStringExtra("Key");
//Log shows value
}
public void restart()
{
onDestroy();
System.exit(0);
}
}

How can I activity close in Android?

I have three classActivity created. One is super class and other sub class and third is HomeActivity.
Code for super class is :
public class MyActivity extends Activity {
Button btnHome = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void onHomeClick(View view) {
String LOG_TAG = "Akshar";
System.out.println("Hello11111");
btnHome = (Button) view;
Log.v(LOG_TAG, "index=" + btnHome);
}
}
and my subclass code is :
public class ChooseIsland extends MyActivity {
Button btn_home = null;
MyActivity ob1 = new MyActivity();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chooseiland);
addListenerOnButton();
}
private void addListenerOnButton() {
// TODO Auto-generated method stub
ob1.onHomeClick(btn_home);
}
}
Now I want to go on Home page when click so when I write ?
Intent intent = new Intent(this, HomeActivity.class);
There is no close operation as such in android. You should make sure you do not save anything in stack so whenever you are traversing from one activity to other, make sure you use intent flags to clear history or top of stack and then call finish.
finish() will close the current activity and the previous activity will come to foreground.
Generally to we write finish() method to close activity.

How to destroy an activity in Android?

While the application is running, I press the HOME button to close the application. When I start the application again, it resumes on the page displayed prior to clicking on HOME. I want the application to start with the initial display instead. I have used finish() to finish the activity but it is not working. Any suggestions?
Most likely you have several instances of the same activity. To resolve this kind of issues create your own parent Activity class e.g. MyRootActivity which will hold static list of all of available/alive activities:
public class MyRootActivity extends Activity
{
private static final String TAG=MyRootActivity.class.getName();
private static ArrayList<Activity> activities=new ArrayList<Activity>();
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
activities.add(this);
}
#Override
public void onDestroy()
{
super.onDestroy();
activities.remove(this);
}
public static void finishAll()
{
for(Activity activity:activities)
activity.finish();
}
}
For that all of your activities need to be children of MyRootActivity.
Then when you are about to sure that you're closing your application - just call MyRootActivity.finishAll();
Create a static Activity object which activity finish on other activity and assign activity in this i.e you can can add more activities
public class demoActivity extends AppCompatActivity {
public static Activity self_intent;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo_activity);
selfintent=this;
}
//Other functions--------------
}
do same for other activities
on other
public class finishingActivity extends AppCompatActivity {
public Button activityCloseBtn;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.finishing_activity);
activityCloseBtn= (Button) view.findViewById(R.id.activity_close_btn);
activityCloseBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
demoActivity.selfintent.finish(); //for finish demoActivityactivity
//for other activities Activity.selfintent.finish();
finish(); //for finish current activity
}
});
try calling super.onPause() first and later call finish() inside your onPause() stub

Categories

Resources