Has anyone successfully used setExitTransition on L? - android

I've been trying to use the new fancy animations that come with the L developer preview, but I'm having a lot of difficulties. In particular, I am not seeing any fancy animations. I'm trying to use the Explode exit transition. Here's the code:
public class ActivityA extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// inside your activity
getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
// set an exit transition
getWindow().setExitTransition(new Explode());
setContentView(R.layout.activity_a);
// Find our button and add our click handler
Button button = (Button)findViewById(R.id.buttonA);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Transition to activity B
Intent intent = new Intent(ActivityA.this, ActivityB.class);
startActivity(intent);
}
});
}
}

Instead of starting another activity using startActivity(intent); use the following statement.
startActivity(intent,ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
I started another activity as said above and it worked for me.
It's said in google documentation Defining Custom Animations as follows.

Solved the problem - you need the
getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
call on the activity you are transitioning too as well!

Try this.
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// enable transitions
getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
setContentView(R.layout.activity_my);
}
public void onSomeButtonClicked(View view) {
getWindow().setExitTransition(new Explode());
Intent intent = new Intent(this, MyOtherActivity.class);
startActivity(intent,
ActivityOptions
.makeSceneTransitionAnimation(this).toBundle());
}
}
requestFeature in onCreate an setExitTransition before startActivity

Related

Dynamically Add button from one activity to another

i am working on an app in which, button Onclick dynamically created a button from one activity and button is appeared in another activity.
So, for your button, see the code below. I'm just using SharedPreferences for testing if button has been clicked before or not.
// Inside your onCreate() method of your SecondActivity.java
((Button)findViewById(R.id.activity2_button)).setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
getSharedPreferences("SharedPreferences", Context.MODE_PRIVATE).edit().putBoolean("ShowButton", true).commit(); // put Boolean inside SharedPreferences
Intent main = new Intent(this, FirstActivity.class);
main.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(main);
finish();
}
}
And now, the FirstActivity.java code :
public class FirstActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1_layout);
if (getSharedPreferences("SharedPreferences", Context.MODE_PRIVATE).getBoolean("ShowButton", false))
((Button)findViewById(R.id.activity1_button)).setVisibility(View.VISIBLE);
else
((Button)findViewById(R.id.activity1_button)).setVisibility(View.GONE);
}
}
Test it, and tell me if this works great. Hope it will work for you, Darkball60 :)

Activity layout blinking after finish() is called

When I open my app, an Activity is started, and inside its onCreate method I'm checking some conditions. If the condition is true, I finish my current Activity and open another one. The problem is: The first activity blinks on the screen and then the second one is opened. The code is below:
public class FirstActivity extends Activity {
#Override
protected final void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//some code here...
checkSomeStuff();
setContentView(R.layout.my_layout);
//some code here...
}
private void checkSomeStuff() {
if (/*some condition*/) {
final Intent intent = new Intent(this, SecondActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
finish();
startActivity(intent);
}
}
}
Notice that setContentView() is after the check, but before the second activity is started, the first one still blinks on the screen.
Does anyone know how to make it not blink?
Thanks in advance.
The purpose of finish() is to destroy the current activity and remove it from the back stack. By calling finish then firing the intent, you are asking the activity to destroy it self (I assume the blink is it trying to recover) then firing the intent to the second. Move finish to after startActivity()
#Override
protected final void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//some code here...
if(checkSomeStuff()) {
setContentView(R.layout.my_layout);
//some code here...
}
}
private boolean checkSomeStuff() {
if (/*some condition*/) {
final Intent intent = new Intent(this, SecondActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
return false;
}
return true;
}
The order of your code is wrong. You should not call anything after
finish();
This is because the activity will be destroyed. Anything following could result in strange behavior.
#Override
protected final void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set my layout
setContentView(R.layout.my_layout);
//some code here...
finish();
//nothing here because activity will be destroyed
}
Flicker and blinking Activity
- Reason finish();
- Remove finish() in Activity
- added android:noHistory="true" in AndroidManifest
The android:noHistory will clear Activity from stack which need to be clear on back press otherwise it shows activity A on screen.
The below-mentioned trick is working perfectly for me. Hope this could useful for others.
Opening activity B from activity A.
To close activity B I am using:
finish();
overridePendingTransition(0, 0);
to avoid the black color blinking problem.
instead of doing
checkSomeStuff();
setContentView(R.layout.my_layout);
you should do
private void checkSomeStuff() {
if (//some condition) {
final Intent intent = new Intent(this, SecondActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
finish();
startActivity(intent);
}else{
setContentView(R.layout.my_layout);
}
}
you see the view because the intent does not fire until onCreate is finished so setContentView gets called
Perhaps you can separate your condition better to entirely avoid setContentView() when you're planning to finish().
public class FirstActivity extends Activity {
#Override
protected final void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (someCondition()) {
goToNextActivity();
} else {
setContentView(R.layout.my_layout);
//some code here...
}
}
private boolean someCondition() {
/* return result of some condition */
}
private void goToNextActivity() {
final Intent intent = new Intent(this, SecondActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
finish();
startActivity(intent);
}
}
Try this with your style
<style name="CHTheme" parent="Theme.AppCompat.Light">
<item name="android:windowDisablePreview">true</item>
</style>

Android back button not going right activity And How to handle Activities in Android

Am creating small application using login, register and user details. After login am going to store login credential to shared preference and navigate to dashboard activity. In second time directly navigate to dashboard activity. This level of code is working fine.
Please consider I have three activity MainActivity, LoginActivity, RegisterActivity, DashboardActivity and ProfileActivity.
In my MainActivity If user value is sharedPreference directly moving to DashboardActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(new SessionManager(getApplicationContext()).isLoggedIn()){
startActivity(new Intent(getApplicationContext(), DashboardActivity.class));
}
}
In my DashboardActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ImageButton imgBtn = (ImageButton) findViewById(R.id.imageButton1);
imgBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(getApplicationContext(), ProfileActivity.class));
}
});
}
Now am simply press back button in device goes to MainActivity not DashboardActivity. I want to move activity to DashboardActivity only, not MainActivity. Please guide me how to do that. And how to handle session in Android.
Also I have bit confusion in using which flags and where to use. I tried in DashboardActivity but not working.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ImageButton imgBtn = (ImageButton) findViewById(R.id.imageButton1);
imgBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent newIntent = new Intent(getApplicationContext(), ProfileActivity.class);
newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
newIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(newIntent);
}
});
}
Now I tried various possible ways but not getting solution.
Main problem occurs after navigating DashboardActivity to any Activity won't come back to DashbarodActivity instead of going MainActivity. I don't know why it happens. Any problem in MainActivity navigation
I'm not sure about that, but in DashboardActivity try to start your activity like this: startActivity(new Intent(DashboardActivity.this, ProfileActivity.class));
Just finish the current activity after starting the new one via Intent and you should achieve the desired functionality and this way you don't need any flags. Except for the MainActivity of course.
Just simply override onBackPressed method in your ProfileActivity and call finish method.
#Override
public void onBackPressed() {
finish();
}
EDITED ANSWER:
on your ProfileActivity, try to override onBackPressed method then start the DashboardActivity again and don't forget to finish the current activity.
#Override
public void onBackPressed(){
startActivity( new Intent(this, DashboardActivity.class) );
finish();
}

Crash when creating Activity

Good evening Stack !
I have started to learn Android development as a hobby and I am now trying to develop my first "real" application (I have made already only five simple applications from books).
In this application, I have two buttons that will "create" the same Activity but by using two different objects from the same base class, hence allowing me to customize the behavior of the application depending on the button that was clicked.
However, when I am trying to create the Intent instance, my application crashes.
Here is the code of the base Activity class
public class BaseDictionnaryActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.basedictionnary);
}
public void onDestroy()
{
super.onDestroy();
}
}
and here is the code that crashes. The line is the one creating the Intent object.
public class DictionnaryActivity extends Activity
{
private BaseDictionnaryActivity jlpt1;
private BaseDictionnaryActivity jlpt2;
private Button btjlpt1 = null;
private Button btjlpt2 = null;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.dictionnary);
jlpt2 = new BaseDictionnaryActivity();
jlpt1 = new BaseDictionnaryActivity();
btJLPT1 = (Button)findViewById(R.id.jlpt1);
btJLPT1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent myIntent = new Intent(DictionnaryActivity.this,
jlpt1.getClass());
jlpt1.this.startActivity(myIntent);
}
});
btJLPT2 = (Button)findViewById(R.id.jlpt2);
btJLPT2.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent myIntent = new Intent(DictionnaryActivity.this,
jlpt2.getClass());
jlpt2.this.startActivity(myIntent);
}
});
}
public void onDestroy()
{
super.onDestroy();
}
}
Thank you for your help !
Just to make correction,
Intent myIntent = new Intent(DictionnaryActivity.this,
jlpt1.getClass());
In this the second argument must be, your target activity BaseDictionnaryActivity.class
So, it looks something like,
Intent myIntent = new Intent(DictionnaryActivity.this,
BaseDictionnaryActivity.class);
startActivity(myIntent);
Also please make sure there will be entry of BaseDictionnaryActivity in your Application's manifest file,
Which is look like,
<activity android:name=".BaseDictionnaryActivity"
....>
</activity>
Maybe:
Intent myIntent = new Intent(DictionnaryActivity.this,
BaseDictionnaryActivity.class);
startActivity(myIntent);
change this
Intent myIntent = new Intent(DictionnaryActivity.this,
NextActivity.class);
Intent myIntent = new Intent(DictionnaryActivity.this,
jlpt2.class);
^^^^^^^^^^^^
You need to provide next activity .class in second argument of Intent.
Replace jlpt1.getClass() with NameOfClassToBeLaunched.class
Also this is bad practice to create Activity instances in other activities.

Android: Button action not being called

I'm trying to make an app where you start at a menu, click a button and are brought to a list of items (which I later hope to make clickable). But I can't seem to make it call my next activity. Can anyone help?
Your main class / activity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Menu Button
Button startNewActivity = (Button)findViewById(R.id.startnew);
startNewActivity.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent newActivityIntent = new Intent(YOUR-CLASS-NAME.this,NewActivity.class);
startActivity(newActivityIntent);
}
});
Your NewActivity Class:
public class NewActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new);
}
}
Is the question "How do I call the next activity" ?
If so, it's pretty easy - Assuming the Activity you want to call is "SomeActivity", call this:
Intent someActivity = new Intent(getBaseContext(), SomeActivity.class);
startActivity(someActivity);
There's also a "startActivityForResult" method, if you want data back from the Activity you're calling. For reference, the Activity page of the API Documentation can be found here. Good luck!

Categories

Resources