I would like that animation on MainActivity to be exhibited only when the user opens the app, but remains static if the user comes from another Activity/Fragment (e.g. by using BottomNavigationMenu), so as not to pollute the window too much.
I think it can be solved by using onCreate, onStart, onResume but I am unable to set it properly (still learning).
The only answer I found is here: https://www.tutorialspoint.com/how-to-launch-activity-only-once-when-android-app-is-opened-for-the-first-time
but it is not what I want since I still would like the animation to be exhibited every time the app opens.
Thank you in advance.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// execute animation here??
}
override fun onStart() {
super.onStart()
setContentView(R.layout.activity_main)
// logic part (buttons) is executed here??
}
Once the Main activity is loaded then you have to finish() from other Activity/Fragment then onResume() override method Main Activity will be called. Do not start Main activity every time if the user comes from another Activity/Fragment.
For fragments:
activity.finish()
For Activity:
finish()
Rather then:
Intent startIntent = new Intent(context, MainActivity.class);
startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startIntent);
Related
Hello I am making a app in which when main activity opens a lotte animation plays as a button and when i click on that animation (button) then activity 2 opens and plays a video and when the video get completes i called main activity again using intent, so when main activity opens again i want that now that animation button hides.
I tried to write override onresume and i also tried override onrestart and then i wrote
override fun onRestart() {
super.onRestart()
binding.btnanimation.visibility = View.INVISIBLE
}
but when i do that then when activity opens in starting then also that button animation get hides, but i want that when app opens it must be visible as its called inside oncreate
my onCreate code something like this
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN)
setContentView(binding.root)
binding.btnanimation.visibility = View.VISIBLE
add android:launchMode="singleInstance" attribute to your MainActivity and don't call finish() on it after starting another activity.
I got a question. I have two activities in my app. In first one, when I click the button, I need something to happen in the second one. How can I do it? If that button would be in the second activity I would just do it by:
button.setOnClickListener {}
But how can I do it when button is in the other activity? It's worth adding that code, that tells what should happen, must be in that second activity, just like it was in that "setOnClickListener". Sorry, I'm starting with Android development.
You could communicate between two activities via broadcast or intent.
But it make logic more complex.
So I suggest use two fragments instead two activities.
If you use two fragment in one activity, you can easy communicate between two fragments.
You can look more detailed information about fragment from this URL.
https://developer.android.com/guide/fragments
To achieve the intended flow you may try the below approach,
Start activity 2 on button click from activity 1.
On activity 2 place your code in onCreate so, once activity 2 loads up your code will fire up.
Activity 1:
button.setOnClickListener{
val intent = Intent(this, second::class.java)
startActivity(intent)
}
Activity 2:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
CallDefinedFunctionHere();
}
You cannot be sure that both activities are present at the same moment since the system might destroy inactive one therefore you cannot trigger any code from activity A inside activity B.
What you can do you can start activity B with an intent and some parameters describing what should happen inside activity B.
or
You can communicate by writing something down to a persistent storage (like SharedPreferences) and then when the other activity is resumed (active again) reading it, reacting to it and then removing it from the storage (to make sure you do not handle it twice).
You can pass data in the intent that opens the second activity.
// In first activity:
buttonX.setOnClickListner {
val intent = Intent(MySecondActivity::class.java).apply {
putExtra("wasFromButtonX", true)
}
startActivity(intent)
}
// In second activity:
fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val wasLaunchedFromButtonX = intent.getBooleanExtra("wasFromButtonX", false)
// Above line uses false as default, so it will only be true if you explicitly
// put the extra in the Intent that started this Activity.
if (wasLaunchedFromButtonX) {
// do alternate setup here
}
}
How to pass information between Activities is explained in the introductory documentation here.
Create a function in class where 2nd activity are defined like this.
public void refresh(){}
Now Call that in your 1st activity where you want to call 1st after any action.
button.setOnClickListener {((MainActivity) Objects.requireNonNull(getActivity())).refresh();}
I've bumped into a strange issue - WebView becomes unresponsive when I finish the activity that started the activity with the WebView BUT it doesn't happen on the first launch, only on the consecutive ones and it also happens on Android 6 and 7, newer Android versions seem to handle that gently.
The full project which reproduces this can be found here
I have two activities Launcher
class LauncherActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
startActivity(Intent(this, MainActivity::class.java))
finish()
}
}
And MainActivity with the WebView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val webView = findViewById<WebView>(R.id.oauthWebView)
webView.loadUrl("https://semantic-ui.com/examples/login.html")
}
}
When I start the app the first time everything seems okay, but the second time when I open it,
WebView becomes unresponsive, the keyboard is not showing and it happens right after Launcher onDestroy
If I'm not calling finish in the LauncherActivity everything is fine.
It looks like finishing LauncherActivity has some impact on WebView loading or some other process that's happening under the hood.
It's pretty weird, Logcat is not very helpful either.
Maybe any of you can provide some hint what might be the reason for such behaviour?
Side note:
I'm not looking for answers on how to architect my navigation better, I know it can be done differently - I'm curious why WebView is behaving so weird.
Can you try this?
val webView = findViewById<WebView>(R.id.oauthWebView)
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://semantic-ui.com/examples/login.html")
I have an initialization activity in my app which displays the logo, then I show my next activity using
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) // Call the parent class function
setContentView(R.layout.activity_launcher)
// This starts a new co-routine
// it is important to do it this way, in order to show the UI _before_
// all the initialization happens, otherwise launcher is pointless
GlobalScope.launch {
...
[initialization]
...
startActivity(ActivityTwo)
}
}
The transition takes about three seconds because of all the code that is running inside onCreate belonging to ActivityTwo. Is there a way to "create" the second activity behind the scenes, and then show it. I don't mind if the app stays on the initialization screen for those 3 seconds, but the white transition looks really ugly.
onCreate method is actually creating your activity. You must be doing some heavy computation if your activity is janking while rendering. If you are not satisfied with the transition between the two activities, then apply an animation between them.
The animation when I am going from a fragment to an activity is working fine but when I click back it returns without the custom animation I insert. The same if I make the navigation from a fragment to another with the same animations works fine. Here is the action code I am using:
<action
android:id="#+id/toTicker"
app:destination="#id/tickerActivity"
app:enterAnim="#anim/slide_bottom_up"
app:exitAnim="#anim/slide_up_bottom"
app:popEnterAnim="#anim/slide_bottom_up"
app:popExitAnim="#anim/slide_up_bottom"/>
As per this issue, you need to call the static ActivityNavigator.applyPopAnimationsToPendingTransition() method in your other activity to get the pop animations to apply - it should be called directly following when you call finish() or as part of callbacks to onBackPressed() (which internally will call finish()):
override fun onBackPressed() {
super.onBackPressed()
ActivityNavigator.applyPopAnimationsToPendingTransition(this)
}
Updating the documentation to specifically call this out is being tracked in this documentation issue.
The previously selected answer doesn't work anymore.
applyPopAnimationsToPendingTransition must be called by overriding Activity.finish().
override fun finish() {
super.finish()
ActivityNavigator.applyPopAnimationsToPendingTransition(this)
}