For Navigation I've navigation.kt
which hold Nav()
that is used to navigate.
There is also a helper function give_route()
that take string then if the it is known (hardcoded) then give its route otherwise page_not_found
route - string for navController
package com.<company_name.<app_name>
import androidx.compose.runtime.Composable
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.<company_name.<app_name>
// Class For function needed for Navigation
@Composable
fun Nav() {
val navController = rememberNavController()
NavHost(navController = navController, startDestination = Address.Feed.route) {
composable(Address.Feed.route)
{
feed(
Navi = {add -> navController.navigate(give_route(add))}
)
}
}
}
fun give_route(title: String): String {
return when (title) {
"Feed" -> Address.Feed.route
"Question" -> Address.Question.route
"Notice" -> Address.Notice.route
"Status" -> Address.Status.route
"DM" -> Address.DM.route
"Chat" -> Address.Chat.route
"Group_chat" -> Address.Group_chat.route
"Notify" -> Address.Notify.route
"Search" -> Address.Search.route
"Profile" -> Address.Profile.route
"Profile-Pvt" -> Address.Profile_Pvt.route
"Profile-User" -> Address.Profile_User.route
"Ratting" -> Address.Ratting.route
"New - Text" -> Address.New_Text.route
"New - Image" -> Address.New_Image.route
"New - Video" -> Address.New_Video.route
"New - Audio" -> Address.New_Audio.route
"New - Other" -> Address.New_File.route
else -> Address.page_not_found.route
}
}
All routes string are stored in Sealed class Address.kt
package com.<company_name.<app_name>
sealed class Address(val route: String) {
object Feed : Address("Feed")
object Question : Address("Question")
object Notice : Address("Notice")
object Status : Address("Status")
object DM : Address("DM")
object Chat : Address("Chat")
object Group_chat : Address("Group_chat")
object Notify : Address("Notify")
object Search : Address("Search")
object Profile : Address("Profile")
object Profile_Pvt : Address("Profile-Pvt")
object Profile_User : Address("Profile-User")
object Ratting : Address("Ratting")
object New_Text : Address("New - Text")
object New_Image : Address("New - Image")
object New_Video : Address("New - Video")
object New_Audio : Address("New - Audio")
object New_File : Address("New - File")
object page_not_found : Address("Page not found")
}
I'm running Nav() from MainActivity.kt
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
AppTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Nav()
}
}
}
}
}
Finall all @Composable
fun. are in Views/Pages.kt
. For the purpose of this question I've hiided all fun except feed
and its Menyitem clled func, for ex. New_Text
package com.<company_name.<app_name>
import androidx.compose.foundation.layout.Row
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
// PAGE VIEWS
// Feed View
@Composable
fun feed(Navi: (String) -> Unit) {
var newPost by remember { mutableStateOf(false) } // TODO - y
Row() {
FilledTonalIconButton(
onClick = { newPost = true },
enabled = true
) {
Icon(
Icons.Rounded.Add,
"Button for creating a new post. " +
"Opens the Menu to selct the type for your new post"
)
}
DropdownMenu(
expanded = newPost,
onDismissRequest = { newPost = false }
) {
DropdownMenuItem(
text = { Text("Text") },
onClick = { Navi("New - Text") },
enabled = true,
leadingIcon = {
Icon(Icons.Rounded.ViewHeadline,
"To Post Text"
)
}
)
DropdownMenuItem(
text = { Text("Image") },
onClick = { Navi("New - Image") },
enabled = true,
leadingIcon = {
Icon(Icons.Rounded.Wallpaper,
"To Post Image"
)
}
)
DropdownMenuItem(text = { Text("Video") },
onClick = { Navi("New - Video") },
enabled = true,
leadingIcon = {
Icon(Icons.Rounded.Slideshow,
"To Post Video"
)
}
)
DropdownMenuItem(text = { Text("Audio") },
onClick = { Navi("New - Audio") },
enabled = true,
leadingIcon = {
Icon(Icons.Rounded.SpatialAudioOff,
"To Post Audio"
)
}
)
DropdownMenuItem(text = { Text("Other") },
onClick = { Navi("New - Other") },
enabled = true,
leadingIcon = {
Icon(Icons.Rounded.PostAdd,
"To open custom file window for posting user file"
)
}
)
}
}
}
@Composable
fun New_Text() {
Text("a")
}
@Composable
fun New_Image() {
Text("b")
}
@Composable
fun New_Video() {
Text("c")
}
@Composable
fun New_Audio() {
Text("d")
}
@Composable
fun New_File() {
Text("e")
}
For dependicies, imports, versions etc. please vist my other question
When I'm running on my debugging device, the feed is getting drawn and Drop Down Menu is axpanding on click but as soon as I press any Menu Button, I get following Error and app Stops.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.<company_name.<app_name>, PID: 20633
java.lang.IllegalArgumentException: Navigation destination that matches request NavDeepLinkRequest{ uri=android-app://androidx.navigation/New - Audio } cannot be found in the navigation graph NavGraph(0x0) startDestination={Destination(0x78c8aacb) route=Feed}
at androidx.navigation.NavController.navigate(NavController.kt:1664)
at androidx.navigation.NavController.navigate(NavController.kt:1984)
at androidx.navigation.NavController.navigate$default(NavController.kt:1979)
at com.<company_name.<app_name>.NavigationKt$Nav$1$1$1.invoke(Navigation.kt:21)
at com.<company_name.<app_name>.NavigationKt$Nav$1$1$1.invoke(Navigation.kt:20)
at com.<company_name.<app_name>.views.PagesKt$feed$1$3$4$1.invoke(Pages.kt:69)
at com.<company_name.<app_name>.views.PagesKt$feed$1$3$4$1.invoke(Pages.kt:69)
at androidx.compose.foundation.ClickableKt$clickable$4$gesture$1$2.invoke-k-4lQ0M(Clickable.kt:153)
at androidx.compose.foundation.ClickableKt$clickable$4$gesture$1$2.invoke(Clickable.kt:142)
at androidx.compose.foundation.gestures.TapGestureDetectorKt$detectTapAndPress$2$1$1.invokeSuspend(TapGestureDetector.kt:222)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTaskKt.resume(DispatchedTask.kt:178)
at kotlinx.coroutines.DispatchedTaskKt.dispatch(DispatchedTask.kt:166)
at kotlinx.coroutines.CancellableContinuationImpl.dispatchResume(CancellableContinuationImpl.kt:397)
at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl(CancellableContinuationImpl.kt:431)
at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl$default(CancellableContinuationImpl.kt:420)
at kotlinx.coroutines.CancellableContinuationImpl.resumeWith(CancellableContinuationImpl.kt:328)
at androidx.compose.ui.input.pointer.SuspendingPointerInputFilter$PointerEventHandlerCoroutine.offerPointerEvent(SuspendingPointerInputFilter.kt:566)
at androidx.compose.ui.input.pointer.SuspendingPointerInputFilter.dispatchPointerEvent(SuspendingPointerInputFilter.kt:456)
at androidx.compose.ui.input.pointer.SuspendingPointerInputFilter.onPointerEvent-H0pRuoY(SuspendingPointerInputFilter.kt:469)
at androidx.compose.ui.node.BackwardsCompatNode.onPointerEvent-H0pRuoY(BackwardsCompatNode.kt:394)
at androidx.compose.ui.input.pointer.Node.dispatchMainEventPass(HitPathTracker.kt:314)
at androidx.compose.ui.input.pointer.Node.dispatchMainEventPass(HitPathTracker.kt:301)
at androidx.compose.ui.input.pointer.Node.dispatchMainEventPass(HitPathTracker.kt:301)
at androidx.compose.ui.input.pointer.Node.dispatchMainEventPass(HitPathTracker.kt:301)
at androidx.compose.ui.input.pointer.Node.dispatchMainEventPass(HitPathTracker.kt:301)
at androidx.compose.ui.input.pointer.NodeParent.dispatchMainEventPass(HitPathTracker.kt:183)
at androidx.compose.ui.input.pointer.HitPathTracker.dispatchChanges(HitPathTracker.kt:102)
at androidx.compose.ui.input.pointer.PointerInputEventProcessor.process-BIzXfog(PointerInputEventProcessor.kt:98)
at androidx.compose.ui.platform.AndroidComposeView.sendMotionEvent-8iAsVTc(AndroidComposeView.android.kt:1329)
at androidx.compose.ui.platform.AndroidComposeView.handleMotionEvent-8iAsVTc(AndroidComposeView.android.kt:1275)
at androidx.compose.ui.platform.AndroidComposeView.dispatchTouchEvent(AndroidComposeView.android.kt:1214)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3170)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2846)
at android.view.View.dispatchPointerEvent(View.java:15384)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:6896)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:6667)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:6114)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:6176)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:6137)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:6311)
E/AndroidRuntime: at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:6145)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:6368)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:6118)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:6176)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:6137)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:6145)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:6118)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:9272)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:9223)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:9172)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:9415)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:276)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:339)
at android.os.Looper.loopOnce(Looper.java:186)
at android.os.Looper.loop(Looper.java:351)
at android.app.ActivityThread.main(ActivityThread.java:8355)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:584)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1013)
Suppressed: kotlinx.coroutines.DiagnosticCoroutineContextException: [androidx.compose.ui.platform.MotionDurationScaleImpl@4373ed0, androidx.compose.runtime.BroadcastFrameClock@5894ec9, StandaloneCoroutine{Cancelling}@9edf9ce, AndroidUiDispatcher@fc450ef]
I/Process: Sending signal. PID: 20633 SIG: 9
Sorry for long Post.
Edit: I answered it in this answer
Peace!