How to Know the Calling Activity in Android

how to know the calling activity in android

A. If you can use startActivityForResult

As per Zain Ali's answer below: If you can start Activity with startActivityForResult() then you can get name of calling Activity class by this.getCallingActivity().getClassName();

B. If you can not use startActivityForResult

If you can not use startActivityForResult(), then you can use following method:
You can pass additional parameter in intent, check the value in activity and act accordingly.

1) Define an interface or constants class to define integer constants to indicate calling activity

public interface ActivityConstants {
public static final int ACTIVITY_1 = 1001;
public static final int ACTIVITY_2 = 1002;
public static final int ACTIVITY_3 = 1003;
}

2) Add extra parameter in intent while calling Activity2.

        Intent act2 = new Intent(context, Activity2.class);
act2.putExtra("calling-activity", ActivityConstants.ACTIVITY_1);
// or ActivityConstants.ACTIVITY_3 if called form Activity3
startActivity(act2);

3) Check the value of this extra parameter in Activity2 and act accordingly..

int callingActivity = getIntent().getIntExtra("calling-activity", 0);

switch (callingActivity) {
case ActivityConstants.ACTIVITY_1:
// Activity2 is started from Activity1
break;
case ActivityConstants.ACTIVITY_3:
// Activity2 is started from Activity3
break;
}

Retrieve calling Activity from Intent in Android

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;

This is the best solution according to my little knowledge. Just pass a code/value for the varification like this.

Intent intent = new Intent();
intent.putExtra("someValue", "data");

And in the second activity check the value, if the value is your required value then get your required activity instance. I hope this will solve your problem and if it will, please let me know.

How to get the name of calling activity?

You can send data between activities using Bundle

Have a look at this example:
http://www.balistupa.com/blog/2009/08/passing-data-or-parameter-to-another-activity-android/

Basically you need to put the name of the caller as parameter:

Bundle bundle = new Bundle();
bundle.putString(this.class.getName(), “ClassName”);

Intent newIntent = new Intent(this.getApplicationContext(), ActivityClass2.class);
newIntent.putExtras(bundle);
startActivityForResult(newIntent, 0);

And in ActivityClass2, you can read this parameter using:

Bundle bundle = this.getIntent().getExtras();
String className = bundle.getString(“ClassName″);

Calling one Activity from another in Android

First question:

Use the Intent to call another Activity. In the Manifest, you should add

<activity android:name="ListViewImage"></activity>
<activity android:name="com.company.listview.ListViewImage">
</activity>

And in your current activity,

btListe = (ImageButton)findViewById(R.id.Button_Liste);
btListe.setOnClickListener(new OnClickListener()
{ public void onClick(View v)
{
intent = new Intent(main.this, ListViewImage.class);
startActivity(intent);
finish();
}
});

Second question:

sendButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String valueString = editValue.getText().toString();
long value;
if (valueString != null) {
value = Long.parseLong(valueString);
}
else {
value = 0;
}

Bundle sendBundle = new Bundle();
sendBundle.putLong("value", value);

Intent i = new Intent(Activity1.this, Activity2.class);
i.putExtras(sendBundle);
startActivity(i);

finish();
}
});

and in Activity2:

 Bundle receiveBundle = this.getIntent().getExtras();
final long receiveValue = receiveBundle.getLong("value");
receiveValueEdit.setText(String.valueOf(receiveValue));
callReceiverButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(Activity2.this, Receiver.class);
i.putExtra("new value", receiveValue - 10);
}
});

How To get The name Of Calling activity by any Started Activity?

Use below code for getting activity name if MainActivity is calling Activity for another Activity

String callingActivityName = MainActivity.class.getSimpleName();

How to know when the call Activity in android is finished

check BroadcastReceiver receiver and system's broadcast messages. This kind of events (starting call, ending call, etc) are usually sent by broadcast message. And, if you have receiver, you can get and do whatever you want. for instance, android.intent.action.PHONE_STATE might work for you.
http://androidexample.com/Introduction_To_Broadcast_Receiver_Basics/index.php?view=article_discription&aid=60&aaid=85

I think you get get information that you want through intent.getExtras

    public void onReceive(Context context, Intent intent) {

intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
int state = 0;
if(stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE)){
state = TelephonyManager.CALL_STATE_IDLE;
}
else if(stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
state = TelephonyManager.CALL_STATE_OFFHOOK;
}
else if(stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)){
state = TelephonyManager.CALL_STATE_RINGING;
}

}

How to know if an activity is called using startActivityForResult or simply called by using startActivity?

I know this question is answered already but I have a better solution..

When your activity was started just by startActivity() a getCallingActivity() method in target activity will return null. When it was called by startActivityForResult() it will return name of calling activity.

See getCallingActivity for more details.

So you can check in Activity before finishing for calling activity. If result is null Activity was called by startActivity() and if result is not null then Activity was called by startActivityForResult(). Thats it.

example :-

if (getCallingActivity() == null) {
//This Activity was called by startActivity
} else {
//This Activity was called by startActivityForResult
}

Call an activity and remove calling activity from stack?

Call

finish();

after sending the intent.

How to find calling class for an Activity in Android Studio?

If your only want to know the activities you are viewing, you could easily do it by viewing Logcat. Open the logcat window (AndroidMonitor -> logcat)

Search for ActivityManager

You wull get the below logs

07-19 15:42:04.064 606-632/system_process I/ActivityManager: Displayed
com.android.dialer/.DialtactsActivity: +674ms

In this case I launched the phone application.

But if you simple want to put some piece of code which executes only if this activity is launched from a particular place then you can do something like this.

Put this code when you launch the activity.

Intent intent = new Intent(context, Activity1.class);
intent.putExtra("ENABLE_XXX_CODE", true);

In the activity where you want to put enable some logic for this particular flow :

boolean isEnabled = savedInstanceState.getString("ENABLE_XXX_CODE");  
if(isEnabled)
{
//Your logic goes here.
}


Related Topics



Leave a reply



Submit