Reputation: 1427
My main activity has a button which launches UnityActivity. I need to finish the UnityActivity and return to previous activity. When pressing the back button it closes the whole application.
What can I do? Thank you!
Edit:
I use the AR player from Qualcomm Augmented Reality (Unity Extension). I have only one main activity which I start the AR player from Qualcomm Augmented Reality.
Main Activity
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onBtnStartClick(final View v) {
Intent i= new Intent(this,ArPart.class);
startActivity(i);
}
}
AR Player Activity
public class ArPart extends QCARPlayerActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
finish();
}
return super.onKeyDown(keyCode, event);
}
}
Upvotes: 4
Views: 5275
Reputation: 1756
I found it easiest to use the overridden onBackPressed()
but to do that you also have to make a change in your Unity project:
void Update ()
{
if (Input.GetKeyUp (KeyCode.Escape)) {
AndroidJavaClass jc = new AndroidJavaClass ("com.unity3d.player.UnityPlayer");
AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject> ("currentActivity");
jo.Call ("onBackPressed");
}
}
Then, in your activity class:
@Override
public void onBackPressed() {
// Handle the activity however you want,
// what you do here will be executed when the back button is pressed
}
Also, remember that for this to work as expected, the UnityPlayer object cannot be paused - you should pause it after you've handled the back button press event.
Credit goes to @Frank Nguyen from this thread: Can't pass back event from Unity to android library jar.
Upvotes: 4
Reputation: 18509
do something like this as follows:
Intent i=new Intent(unityActivity.this,mainActivity.class); // the names of activity as per you program.
startActivity(i);
finish();
Upvotes: 0
Reputation: 3882
use the following code
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
//super.onBackPressed();
finish();
}
Upvotes: 0